How To Call A Taglib As A Function In A Domain Class

后端 未结 2 1726
予麋鹿
予麋鹿 2021-01-02 07:30

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:



        
2条回答
  •  伪装坚强ぢ
    2021-01-02 08:12

    Most taglibs rely on data from the controller so it's often not possible to reuse them while others concern view logic so often it's not something you would want to put in a domain class.

    That said, I'm sure you have your reasons so maybe the source of the taglib will help:

    class ResourceTagLib  {
    
        def externalResourceServerService
    
        def resourceLinkTo = { attrs ->
            out << externalResourceServerService.uri
            out << '/'
            if(attrs['dir']) {
                out << "${attrs['dir']}/"
            }
            if(attrs['file']) {
                out << "${attrs['file']}"
            }
        }
    }
    

    ie inject the externalResourceServerService into your domain class and the rest should be simple.

提交回复
热议问题