How to override standard behavior of ApplicationTagLib#createLink and g:link?

后端 未结 3 1102
攒了一身酷
攒了一身酷 2021-01-02 04:03

Background: I have grails 1.3.7 application which uses g:createLink and g:link on many pages.

Recently I decided to make b

3条回答
  •  有刺的猬
    2021-01-02 04:57

    I had a smilar problem. Actually you can decorate g:link tag like this.

    1) TagLib

    import org.codehaus.groovy.grails.plugins.web.taglib.*
    
    class OverrideDefaultTagLib {
    
        static namespace = "g"
    
        def link = { attrs, body ->
    
            def c = "1" // Get it from session or somewhere else
    
            if (attrs.params) {
                attrs.params.put("region", c)
            } else {
                attrs.params = [region: c]
            }
    
            def applicationTagLib = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
                applicationTagLib.link.call(attrs, body)
            }
        }
    }
    

    2) add to UrlMappings.groovy

    "/$region/$controller/$action?/$id?"{}
    

提交回复
热议问题