如何访问域类中的 g.-命名空间
我想在域类的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用:
或者您可以直接获取
messageSource
:ApplicationHolder.application.mainContext.getBean('messageSource')
You can use:
or else you can get
messageSource
directly:ApplicationHolder.application.mainContext.getBean('messageSource')
在 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.