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
myparameter.something=My Value
So, if we want to get the ‘My Value’ value on a *.gpg page we just need write:
${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:
import org.springframework.context.i18n.LocaleContextHolder as LCH
class MyClass {
 /** Dependency injection for getMessage. */
 def messageSource

 public def getMyParameter(){
  return messageSource.getMessage('myparameter.something', null, 'Some default value', LCH.getLocale())
 }
}
That's all folks!

No comments:

Post a Comment

Post a Comment