grails - 如何在域类中将Taglib调用为函数

标签 grails grails-plugin

我需要从我的域类中调用静态资源插件(http://www.grails.org/Static+Resources+Plugin)。

这在 Controller 中完美地工作:

 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称为函数?

最佳答案

前一段时间,我正在开发的一个应用程序遇到了这个问题。我最终要做的是在服务方法中调用标记:

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)
    }
}

关于grails - 如何在域类中将Taglib调用为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2159423/

相关文章:

javascript - grails ajax文件上传:form

jquery - 如何使用以下代码从Grails中的jquery代码将参数传递给 Controller ​​操作

grails - Grails域中的自定义16位ID生成器

grails - 将图像放在Grails 2.4框架中的何处

Grails 构建 war 文件失败,但没有说明原因

grails - 一个Grails应用程序中有多个Spring Batch作业

grails - 如何将服务动态注入(inject)到 grails 自定义工件中

grails - Grails 3无法解析本地插件

spring - 编译期间发生 fatal error org.springframework.beans.factory.BeanDefinitionStoreException