Thursday, January 3, 2013

Grails: How to Read Properties Files in a Groove Class

Reading value from a *.properties file on a *.gpg page is very easy. Also, the scaffold generated CRUD pages have a lot of example what can be consulted as a start point.
Let’s suppose that we have a <my project>/i18n/parameters.properties file which has line
  1. myparameter.something=My Value
So, if we want to get the ‘My Value’ value on a *.gpg page we just need write:
  1. ${message(code: 'myparameter.something', default: 'Some default value')}
But, what to do if it’s necessary reading a property’s value in a Groove Class? The answer is quite easy! Supposing the same parameters.properties file we supposed before, we can do:
  1. import org.springframework.context.i18n.LocaleContextHolder as LCH
  2. class MyClass {
  3. /** Dependency injection for getMessage. */
  4. def messageSource
  5.  
  6. public def getMyParameter(){
  7. return messageSource.getMessage('myparameter.something', null, 'Some default value', LCH.getLocale())
  8. }
  9. }
That's all folks!

No comments:

Post a Comment