如何将Taglib作为域类中的函数调用 [英] How To Call A Taglib As A Function In A Domain Class
问题描述
我需要调用Static Resources Plugin( http://www.grails.org/静态+资源+插件)。
这在控制器中完美运行:
def tstLink = resourceLinkTo(dir:docs / $ {identifier},file:originalFileName)
但是在我得到的域类中
异常消息:没有方法的签名:static org.maflt.ibidem.Item.resourceLinkTo()适用于参数类型:(java.util.LinkedHashMap)values:[[dir:docs / 19e9ea9d-5fae-4a35-80a2-daedfbc7c2c2,file:2009-11-12_1552。 png]]
我认为这是一个普遍问题。
那么如何将一个taglib作为一个函数调用到一个领域类中?解析方案
我遇到了这个问题而之前我正在开发一个应用程序。我最终做的是在服务方法中调用标签:
class MyService {
def grailsApplication // autowired spring
def methodThatUsesATag(identifier,originalFileName){
//这是默认的grails标记库
def g = grailsApplication.mainContext.getBean('org。 codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
g.resourceLinkTo(dir:docs / $ {identifier},file:originalFileName)
}
}
然后在我的域类中,我也可以通过spring autowiring获得服务:
class MyDomain {
String originalFileName
def myService // autowired
static transients = ['myService'] //必要的,这样GORM不会尝试持久服务实例。
//您可以在此处创建一个方法,使用您的
//服务从域实例返回所需的内容。
def myMethod(){
myService.methodThatUsesATag(id,originalFileName)
}
}
I need to call the Static Resources Plugin (http://www.grails.org/Static+Resources+Plugin) from my domain class.
This works perfectly in a controller:
def tstLink = resourceLinkTo(dir:"docs/${identifier}",file:originalFileName)
but in a domain class I get
Exception Message: No signature of method: static org.maflt.ibidem.Item.resourceLinkTo() is applicable for argument types: (java.util.LinkedHashMap) values: [[dir:docs/19e9ea9d-5fae-4a35-80a2-daedfbc7c2c2, file:2009-11-12_1552.png]]
I assume this is a general problem.
So how do you call a taglib as a function in a domain class?
I encountered this problem a while ago for an app I was working on. What I ended up doing was putting a call to the tag in a service method:
class MyService {
def grailsApplication //autowired by spring
def methodThatUsesATag(identifier, originalFileName) {
//This is the default grails tag library
def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
g.resourceLinkTo(dir:"docs/${identifier}",file:originalFileName)
}
}
Then in my domain class, I could get to the service via spring autowiring as well:
class MyDomain {
String originalFileName
def myService //autowired
static transients = ['myService'] //Necessary so that GORM doesn't try to persist the service instance.
//You can create a method at this point that uses your
//service to return what you need from the domain instance.
def myMethod() {
myService.methodThatUsesATag(id, originalFileName)
}
}
这篇关于如何将Taglib作为域类中的函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!