如何在域类中访问g.-名称空间 [英] How to access the g.-namespace in a Domain Class
本文介绍了如何在域类中访问g.-名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的域类的toString方法中使用g.message()功能,但是默认情况下无法访问g.-名称空间.
我怀疑import g.*
是否能解决问题.
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.
我已经知道我可以使用messageSource
功能,但是最好使用与视图中相同的语法.
I already know that I can use the messageSource
functionality, but it would be nicer to use the same syntax as in the views.
推荐答案
您可以使用:
class MyDomain {
def someMethod() {
def g = ApplicationHolder.application.mainContext.getBean( 'org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib' )
return g.message(....)
}
}
,否则您可以直接获取messageSource
:ApplicationHolder.application.mainContext.getBean('messageSource')
or else you can get messageSource
directly: ApplicationHolder.application.mainContext.getBean('messageSource')
这篇关于如何在域类中访问g.-名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文