3

我是 Grails 的新手,所以你可能会觉得我的问题很幼稚。我想覆盖 g 消息标签。我想在标签的行为中添加新功能,然后从 ValidationTagLib 调用原始实现。就我而言,我可以创建自己的标签库,在其中重新定义标签。G message 是一个闭包,它实际上调用了 ValidationTaglib 中的 messageImpl 方法。我的问题是如何调用此方法?我尝试使用此代码来调用闭包,但我收到的不是消息,而是空格:

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

非常感谢您的帮助!

4

1 回答 1

7

您需要扩展 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)
    }
}
于 2013-02-27T13:07:49.547 回答