2013-02-26 19 views
2

我在Grails的新手,所以你可能會發現我的問題很幼稚。 我想覆蓋g消息標記。我想在標籤的行爲中添加新功能,然後從ValidationTagLib中調用原始實現。 就我而言,我可以創建自己的taglib,並在其中重新定義標籤。 G消息是實際從ValidationTaglib調用messageImpl方法的閉包。 我的問題是我該如何調用這個方法? 我嘗試這樣的代碼來調用關閉,但不是我的消息有收到空的空間:的Grails如何覆蓋摹消息標記

class MyTagLib { 

    static namespace = "g" 

    def message = { attrs -> 
     //my changes in tag's behaviour 
     def validationTagLib = grailsAttributes.applicationContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib') 
     validationTagLib.message.call(attrs) 
    } 
} 

我將非常感謝您的幫助!

+0

是'MyTagLib.groovy'在正確的目錄(即'的grails-app/taglib')?你有沒有用2個參數(attrs,body)進行封閉嘗試? – Isammoc 2013-02-26 20:53:49

+0

MyTagLib.groovy是在正確的目錄中,我曾試圖關閉以2個參數,但還是沒有結果 – user2109125 2013-02-27 10:51:16

+0

看到http://stackoverflow.com/questions/6230852/how-to-override-standard-behavior-of-applicationtaglibcreatelink-and -glink – 2013-02-27 12:25:16

回答

5

您需要延長的Grails ValidationTagLib

import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib 

class MyValidationTagLib extends ValidationTagLib { 

    /** 
    * Resolves a message code for a given error or code from the resource bundle. 
    * 
    * @emptyTag 
    * 
    * @attr error The error to resolve the message for. Used for built-in Grails messages. 
    * @attr message The object to resolve the message for. Objects must implement org.springframework.context.MessageSourceResolvable. 
    * @attr code The code to resolve the message for. Used for custom application messages. 
    * @attr args A list of argument values to apply to the message, when code is used. 
    * @attr default The default message to output if the error or code cannot be found in messages.properties. 
    * @attr encodeAs The name of a codec to apply, i.e. HTML, JavaScript, URL etc 
    * @attr locale override locale to use instead of the one detected 
    */ 
    Closure message = { attrs -> 
     //my changes in tag's behaviour 
     ValidationTagLib validationTagLib = grailsAttributes.applicationContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib') 
     validationTagLib.message.call(attrs) 
    } 
} 
+0

謝謝drorb!我本來應該猜到了。我是一個小白菜...... – user2109125 2013-02-27 13:17:48