Constants file in Grails??

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Constants file in Grails??

bobalex
My controller has lot of attributes with hard coded values. I am looking at putting all of them in a constants file like the way we put constants.java in java application.

In Grails, can we do something similar?
Any inputs?

Thanks!

-Bob
Reply | Threaded
Open this post in threaded view
|

Re: Constants file in Grails??

burtbeckwith
Use Config.groovy. It has the advantage of allowing different values for each environment, and to be overridden using external configs. Access the values using 'grailsApplication.config.foo' or 'ConfigurationHolder.config.foo'.

Burt

>
> My controller has lot of attributes with hard coded values. I am looking at
> putting all of them in a constants file like the way we put constants.java
> in java application.
>
> In Grails, can we do something similar?
> Any inputs?
>
> Thanks!
>
> -Bob
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Constants file in Grails??

bobalex
Thanks Burt. But, just wanted to know if that is the best way to do it. I mean I do not have different values for dev, test et al. the values all are same for all the environments. In that case, is there a more generic way. probably like creating a constants.groovy.

If yes, what command is there to create it and how do u access it?
Reply | Threaded
Open this post in threaded view
|

Re: Constants file in Grails??

lucastex
you can just create a constants file just how you did in your java project...

you can also copy the same java file under src/java dir.

[]s,

Lucas Frare Teixeira .·.
- [hidden email]
- lucastex.com.br
- blog.lucastex.com
- twitter.com/lucastex


On Wed, Oct 6, 2010 at 9:18 PM, bobalex <[hidden email]> wrote:

Thanks Burt. But, just wanted to know if that is the best way to do it. I
mean I do not have different values for dev, test et al. the values all are
same for all the environments. In that case, is there a more generic way.
probably like creating a constants.groovy.

If yes, what command is there to create it and how do u access it?
--
View this message in context: http://grails.1312388.n4.nabble.com/Constants-file-in-Grails-tp2965205p2966000.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Constants file in Grails??

burtbeckwith
In reply to this post by bobalex
Just like any class in a non-Grails app. If you have public static fields then you can access them directly:

class Constants {
   static final String FOO = 'bar'
   static final int WHATEVER = 123
}

...

class SomeClass {
   void someMethod() {
      int whatever = Constants.WHATEVER
   }
}

Burt

>
> Thanks Burt. But, just wanted to know if that is the best way to do it. I
> mean I do not have different values for dev, test et al. the values all are
> same for all the environments. In that case, is there a more generic way.
> probably like creating a constants.groovy.
>
> If yes, what command is there to create it and how do u access it?
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Constants file in Grails??

Lim Chee Kin
Hi Burt,

I created the Constants class in src/groovy directory, but when I import the Constants class and access to Constants.FOO in the RunScript.groovy located in scripts directory. When I run the script, it throws "Unresolved Constants ....".

Please advice. Thanks.

Regards,
Chee Kin
Reply | Threaded
Open this post in threaded view
|

Re: Constants file in Grails??

bobalex
Thanks guys. I was able to create the constants file.

I am not sure if its the right way, but it worked for me.
I create a file EmployeeConstants.groovy under service folder.Luckily I have just 1 controller file in my appn

package emp

class EmployeeConstants {
       
                public static final String DEFAULT_EMPLOYEE_DIVISION = V
}
In my service classes and controller classes, i referred to the data  as

def employeeDivison = EmployeeConstants.DEFAULT_EMPLOYEE_DIVISION

there is a catch here. Any changes I do in my constants file doesnt reflect once I refresh my browser. I need to restart tomcat again.donno wats the issue.


Thanks!

Bob
Reply | Threaded
Open this post in threaded view
|

Re: Constants file in Grails??

johnrellis
If your file was an artefact, i.e. a service, you could change it at run time during dev and the changes should reflect.

Changes to regular java and groovy classes are not reflected without a restart, just services, controllers and views (and custom artefacts).

Even though you have your constants file under the services dir it is not a service unless it ends with Service.....  Maybe you could do ConstantsService......  I am not saying it is a good idea or follows sound practice but you have the knowledge, now do with it what you will :)

P.S.  I know for sure that when I change service methods during dev, the changes reflect... I assume the same is for fields, not sure if 'final' will cause any issues though....

HTH

John

On Fri, Oct 8, 2010 at 8:34 AM, bobalex <[hidden email]> wrote:

Thanks guys. I was able to create the constants file.

I am not sure if its the right way, but it worked for me.
I create a file EmployeeConstants.groovy under service folder.Luckily I have
just 1 controller file in my appn

package emp

class EmployeeConstants {

               public static final String DEFAULT_EMPLOYEE_DIVISION = V
}
In my service classes and controller classes, i referred to the data  as

def employeeDivison = EmployeeConstants.DEFAULT_EMPLOYEE_DIVISION

there is a catch here. Any changes I do in my constants file doesnt reflect
once I refresh my browser. I need to restart tomcat again.donno wats the
issue.


Thanks!

Bob
--
View this message in context: http://grails.1312388.n4.nabble.com/Constants-file-in-Grails-tp2965205p2967961.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
John Rellis
@johnrellis

Reply | Threaded
Open this post in threaded view
|

НА: Re: Constants file in Grails??

ishe
In reply to this post by bobalex
One small note:
In my apps I mark constants file as abstract too.



От: bobalex [via Grails] <[hidden email]>
Отправлено: 8 октября 2010 г. 10:34
Кому: ig78 <[hidden email]>
Тема: Re: Constants file in Grails??

Thanks guys. I was able to create the constants file.

I am not sure if its the right way, but it worked for me.
I create a file EmployeeConstants.groovy under service folder.Luckily I have just 1 controller file in my appn

package emp

class EmployeeConstants {
       
                public static final String DEFAULT_EMPLOYEE_DIVISION = V
}
In my service classes and controller classes, i referred to the data  as

def employeeDivison = EmployeeConstants.DEFAULT_EMPLOYEE_DIVISION

there is a catch here. Any changes I do in my constants file doesnt reflect once I refresh my browser. I need to restart tomcat again.donno wats the issue.


Thanks!

Bob


View message @ http://grails.1312388.n4.nabble.com/Constants-file-in-Grails-tp2965205p2967961.html
To start a new topic under Grails - user, email [hidden email]
To unsubscribe from Grails - user, click here.

Reply | Threaded
Open this post in threaded view
|

Re: Constants file in Grails??

Dean Del Ponte-2
In reply to this post by bobalex
If it's not a service, don't put it in the service folder, put it in your src/groovy folder



On Fri, Oct 8, 2010 at 2:34 AM, bobalex <[hidden email]> wrote:

Thanks guys. I was able to create the constants file.

I am not sure if its the right way, but it worked for me.
I create a file EmployeeConstants.groovy under service folder.Luckily I have
just 1 controller file in my appn

package emp

class EmployeeConstants {

               public static final String DEFAULT_EMPLOYEE_DIVISION = V
}
In my service classes and controller classes, i referred to the data  as

def employeeDivison = EmployeeConstants.DEFAULT_EMPLOYEE_DIVISION

there is a catch here. Any changes I do in my constants file doesnt reflect
once I refresh my browser. I need to restart tomcat again.donno wats the
issue.


Thanks!

Bob
--
View this message in context: http://grails.1312388.n4.nabble.com/Constants-file-in-Grails-tp2965205p2967961.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Constants file in Grails??

burtbeckwith
Even if you get it to reload, keep in mind that constants are inlined by the compiler. So what gets compiled into the calling class is

   def employeeDivison = 'V'

and not a runtime reference to the value like you'd get if it were an object.

Burt

> If it's not a service, don't put it in the service folder, put it in your
> src/groovy folder
>
>
>
> On Fri, Oct 8, 2010 at 2:34 AM, bobalex <[hidden email]> wrote:
>
> >
> > Thanks guys. I was able to create the constants file.
> >
> > I am not sure if its the right way, but it worked for me.
> > I create a file EmployeeConstants.groovy under service folder.Luckily I
> > have
> > just 1 controller file in my appn
> >
> > package emp
> >
> > class EmployeeConstants {
> >
> >                public static final String DEFAULT_EMPLOYEE_DIVISION = V
> > }
> > In my service classes and controller classes, i referred to the data  as
> >
> > def employeeDivison = EmployeeConstants.DEFAULT_EMPLOYEE_DIVISION
> >
> > there is a catch here. Any changes I do in my constants file doesnt reflect
> > once I refresh my browser. I need to restart tomcat again.donno wats the
> > issue.
> >
> >
> > Thanks!
> >
> > Bob
> > --
> > View this message in context:
> > http://grails.1312388.n4.nabble.com/Constants-file-in-Grails-tp2965205p2967961.html
> > Sent from the Grails - user mailing list archive at Nabble.com.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >    http://xircles.codehaus.org/manage_email
> >
> >
> >
>

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Constants file in Grails??

Lim Chee Kin
Hi there,

No one here facing the error I mentioned in previous post? Re-post the message here:

I created the Constants class in src/groovy directory, but when I import the Constants class and access to Constants.FOO in the RunScript.groovy located in scripts directory. When I run the script, it throws "Unresolved Constants ...." compilation error.

Hope to hear from you soon!

Regards,
Chee Kin
Reply | Threaded
Open this post in threaded view
|

Re: Constants file in Grails??

burtbeckwith
You have to dynamically load application classes in scripts:

   Constants = classLoader.loadClass('com.foo.bar.Constants')
   String foo = Constants.FOO

You probably need to to include _GrailsPackage:

   includeTargets << grailsScript("_GrailsPackage")

and call classpath and loadApp targets:
 
   depends(classpath, loadApp)

Burt

>
> Hi there,
>
> No one here facing the error I mentioned in previous post? Re-post the
> message here:
>
> I created the Constants class in src/groovy directory, but when I import the
> Constants class and access to Constants.FOO in the RunScript.groovy located
> in scripts directory. When I run the script, it throws "Unresolved Constants
> ...." compilation error.
>
> Hope to hear from you soon!
>
> Regards,
> Chee Kin
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email