4

I have a variable in my Grails app’s BootStrap.groovy:

      class BootStrap {
      def init = { servletContext ->
      def testGrails = "11"

I want to show a JavaScript alert() if this test value is 11.

My JavaScript:

           <SCRIPT LANGUAGE="JavaScript">
           if(testGrails==11)   // WHAT TO WRITE WITH IN THE BRACKETS ...?
           {
               alert("yes");
           }        

How can I access a Grails class in Javascript to do the above?

Thanks.

3 Answers 3

10

First of all - you have to make this variable accessible from other places. Currently it's bounded to init scope. You can put it into global config for example (see Config.groovy). Or set to an service. Or make an public static variable somewhere.

Example for a service:

class VariableHOlderService {

    def testGrails

}

and

class BootStrap {

      VariableHolderService variableHolderService

      def init = { servletContext ->
          VariableHolderService.testGrails = "11"
}

Second - you need to put it into request. There is two ways - use filter or controller/action. First option is useful when you want to use same variable from different GSP.

From controller it would be:

VariableHolderService variableHolderService

def myAction = {
    render model: [testGrails:variableHolderService.testGrails]
}

and use it at GSP as

<g:javascript>
       if(${testGrails}==11)   
       {
           alert("yes");
       }    
</g:javascript>
Sign up to request clarification or add additional context in comments.

2 Comments

hi, thanks i did the same as u mentioned , all four but in gsp alert is : undefined ... any idea
Thank you Igor! ... I prefer to include JS vars in GSP(Sitemesh layout): <g:script> var someJSvar = ${anyAvailableExpression}; </g:script>
2

You should define your variable in Config.groovy, not in Bootstrap. Then you can access it in gsp files like this:

       <SCRIPT LANGUAGE="JavaScript">
       if(${grailsApplication.config.testGrails}==11)
       {
           alert("yes");
       }        

2 Comments

how to use ${grailsApplication.config.testGrails} in external javascript file?
I imagine you can have one GSP file where you have all the variables that you need, and then include it with jQuery in all the other files where you need to access the variables.
1

Are you sure you need it in Bootstrap.groovy? Is it something you calculate or can change?

If your answer is no, I found that the meta tag is very useful for getting information in GSP files.

For example, if you want your application name, you can get it like this:

<g:meta name="app.name"/>

You can get any property in your application.properties file like that.

And if you, like me, need to concatenate it to another value, here is my example. Remember that any tag can be used as a method without the g: namespace. For example:

<g:set var="help" value="http://localhost:8080/${meta(name:"app.name")}/help" />

Grails documentation about this is a little poor, but it is here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.