我需要从我的域类调用静态资源插件(http://www.grails.org/Static+Resources+Plugin)。
这在控制器中完美运行:
def tstLink = resourceLinkTo(dir:"docs/${identifier}",file:originalFileName)
但在域类中我得到了
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]]
我认为这是一个普遍问题。
那么如何将taglib称为域类中的函数?
答案 0 :(得分:11)
前一段时间我遇到过这个问题我正在处理的应用程序。我最终做的是在服务方法中调用标记:
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)
}
}
然后在我的域类中,我也可以通过spring autowiring获得服务:
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)
}
}
答案 1 :(得分:-1)
大多数taglib依赖于来自控制器的数据,因此通常无法重用它们,而其他人则关注视图逻辑,因此通常不会想要放入域类中。
那就是说,我确定你有理由,所以也许taglib的来源会有所帮助:
class ResourceTagLib {
def externalResourceServerService
def resourceLinkTo = { attrs ->
out << externalResourceServerService.uri
out << '/'
if(attrs['dir']) {
out << "${attrs['dir']}/"
}
if(attrs['file']) {
out << "${attrs['file']}"
}
}
}
即将externalResourceServerService注入您的域类,其余的应该是简单的。