Grails - getting a message value from controller

后端 未结 2 403
旧巷少年郎
旧巷少年郎 2020-12-08 00:49

How can I get a value from message properties outside of GSPs? For instance, the equivalent of


but

相关标签:
2条回答
  • 2020-12-08 01:20

    Inside a controller or a taglib, you can use the following :

    g.message(code: 'some.message')
    

    However, inside domain classes or services, you need to inject messageSource and call getMessage() method from Sping class AbstractMessageSource. This snippet shows you how to do that:

    import org.springframework.context.i18n.LocaleContextHolder as LCH
    ...
    class MyServiceOrMyDomain {
      def messageSource 
      ...
      messageSource.getMessage(code, msgArgs, defaultMsg, LCH.getLocale())
      ...
    }
    
    0 讨论(0)
  • 2020-12-08 01:30

    You can also import the validation tag lib and use it grab the message source.

    import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib
    def g = new ValidationTagLib()
    g.message(error: error)
    
    0 讨论(0)
提交回复
热议问题