如何访问域类中的 g.-命名空间

发布于 2024-11-27 05:29:13 字数 171 浏览 0 评论 0原文

我想在域类的 toString 方法中使用 g.message() 功能,但默认情况下无法访问 g.-namespace。 我怀疑 import g.* 是否能解决问题。

我已经知道我可以使用 messageSource 功能,但最好使用与视图中相同的语法。

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 doubt 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲娇萝莉攻 2024-12-04 05:29:13

您可以使用:

class MyDomain {

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

}

或者您可以直接获取 messageSourceApplicationHolder.application.mainContext.getBean('messageSource')

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')

姜生凉生 2024-12-04 05:29:13

在 grails 服务中使用 g.render 有一些提示:在服务中使用“g:”。我还没有对此进行测试,但它在域类中的工作原理应该基本相同,但有一个重要的例外:域类不能使用 InitializingBean,因为它不是驻留在应用程序上下文中的 bean。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文