简体   繁体   English

jsp标记中的动态属性

[英]Dynamic attributes in a jsp tag

I want to have a tag with dynamic attributes, like simple html tags, eg something like this: 我想要一个带有动态属性的标签, 比如简单的html标签, 例如

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

And in my implementation of tag I want to have something like this: 在我的标签实现中我希望有这样的东西:

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

I want to point out that these dynamic attributes are going to be written by hands in a jsp, not just passed as Map like ${someMap} . 我想指出这些动态属性将由jsp中的手写入,而不是像${someMap}那样传递给Map。 So is there any way to achieve this? 有没有办法实现这个目标?

You will have to enable dynamic attributes in your TLD, like so: 您必须在TLD中启用动态属性,如下所示:

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

And then have your tag handler class implement the DynamicAttributes interface: 然后让您的标记处理程序类实现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
    }
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM