开发者

How to access the g.-namespace in a Domain Class

开发者 https://www.devze.com 2023-03-25 11:12 出处:网络
I would like to make use of the g.message() functionality in the toString method of my domain class, but the g.-namespace is not accessible by default.

I would like to make use of the g.message() functionality in the toString method of my domain class, but the g.-namespace is not accessible by default. I do开发者_JS百科ubt that a import g.* will do the trick.

I already know that I can use the messageSource functionality, but it would be nicer to use the same syntax as in the views.


You can use:

class MyDomain {

  def someMethod() {
    def g = ApplicationHolder.application.mainContext.getBean( 'org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib' )
    return g.message(....)
  }

}

or else you can get messageSource directly: ApplicationHolder.application.mainContext.getBean('messageSource')


Using g.render in a grails service has some hints how to use "g:" in a service. I have not tested this, but it should work mostly the same in domain classes, with one important exception: a domain class cannot use InitializingBean since it's not a bean residing in the application context.

0

精彩评论

暂无评论...
验证码 换一张
取 消