我想要一个包含动态属性的标记,例如简单的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。有没有办法实现这个目标?
答案 0 :(得分:4)
您必须在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
}
...
}