如何在域类中将 Taglib 作为函数调用 [英] How To Call A Taglib As A Function In A Domain Class

查看:16
本文介绍了如何在域类中将 Taglib 作为函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用静态资源插件(http://www.grails.org/静态+资源+插件)来自我的域类.

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)

但在域类中我得到

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.

那么如何在域类中将 taglib 作为函数调用?

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

然后在我的域类中,我也可以通过 spring 自动装配访问服务:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆