9

ドメイン クラスから静的リソース プラグイン ( 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 をドメイン クラスの関数として呼び出すのでしょうか?

4

2 に答える 2

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

次に、私のドメインクラスでは、春の自動配線を介してサービスにアクセスすることもできます。

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)
    }
}
于 2010-01-29T14:22:11.683 に答える
-1

ほとんどの taglibs はコントローラーからのデータに依存しているため、それらを再利用できないことがよくありますが、他のものはビュー ロジックに関係しているため、ドメイン クラスに入れたいものではないことがよくあります。

そうは言っても、あなたには理由があると確信しているので、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 を注入すれば、あとは簡単です。

于 2010-01-29T10:36:02.353 に答える