Grails 3从服务调用taglib [英] Grails 3 call taglib from a service
本文介绍了Grails 3从服务调用taglib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从服务中使用grails内部taglib,但收到此错误:
I'm trying to use grails internal taglib from a service but I receive this error:
No signature of method: MyService.message() is applicable for argument types: (java.util.LinkedHashMap) values: [[code:default.app.name]]
这是我正在使用的代码:
This is the code I'm using:
class MyService {
def myMethod() {
def appName = message(code: 'default.app.name')
}
}
推荐答案
Grails 3
解决方案是注入 grailsApplication
对象并使用其上下文获取
The solution is to inject the grailsApplication
object and use its context to get the taglib bean:
class MyService {
def grailsApplication
def myMethod() {
def g = grailsApplication.mainContext.getBean('org.grails.plugins.web.taglib.ApplicationTagLib')
def appName = g.message(code: 'default.app.name')
}
}
Grails 2
With older grails version the class package is slightly different:
def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
如果您尝试在更新的grails版本(3+)中使用此软件包,则会收到错误消息:
If you try to use this package with newer grails version (3+), you'll receive an error:
No bean named 'org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib' is defined.
自定义标签库
对于任何版本,如果您需要使用服务中的自定义taglib,只需使用以下代码即可:
With any version, if you need to use your custom taglib from a service just use a code like this:
def c = grailsApplication.mainContext.getBean('my.pkg.HelpfulTagLib')
这篇关于Grails 3从服务调用taglib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文