jsp标签中的动态属性

我想要一个带有动态属性的标签, 比如简单的html标签, 例如

<tags:superTag dynamicAttribute1="value" someOtherAttribute="valueOfSomeOther"/>

在我的标签实现中,我想要这样的东西:

public class DynamicAttributesTag {

    private Map<String,String> dynamicAttributes;

    public Map<String, String> getDynamicAttributes() {
        return dynamicAttributes;
    }

    public void setDynamicAttributes(Map<String, String> dynamicAttributes) {
        this.dynamicAttributes = dynamicAttributes;
    }

    @Override
    protected int doTag() throws Exception {
        for (Map.Entry<String, String> dynamicAttribute : dynamicAttributes.entrySet()) {
            // do something
        }
        return 0;
    }
}

我想指出的是,这些动态属性将被用手写在一个jsp中,而不是像${someMap}那样通过Map来传递。 那么有什么办法可以做到这一点?


您必须在您的TLD中启用动态属性,如下所示:

<tag>
    ...
    <dynamic-attributes>true</dynamic-attributes>
</tag>

然后让你的标签处理器类实现DynamicAttributes接口:

public class DynamicAttributesTag extends SimpleTagSupport implements DynamicAttributes {
    ...
    public void setDynamicAttribute(String uri, String localName, Object value) throws JspException {
        // This gets called every time a dynamic attribute is set
        // You could add the (localName,value) pair to your dynamicAttributes map here
    }
    ...
}
链接地址: http://www.djcxy.com/p/85691.html

上一篇: Dynamic attributes in a jsp tag

下一篇: XML file with &auml;