本文介绍了jsp标记中的动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个带有动态属性的标签,例如简单的html标签,例如 :
I want to have a tag with dynamic attributes, like simple html tags, e.g. something like this:
<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;
}
}
我想指出的是,这些动态属性将由jsp手动编写,而不仅仅是像${someMap}
这样作为Map传递.那么有什么办法可以做到这一点?
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}
. So is there any way to achieve this?
推荐答案
您将必须在TLD中启用动态属性,就像这样:
You will have to enable dynamic attributes in your TLD, like so:
<tag>
...
<dynamic-attributes>true</dynamic-attributes>
</tag>
然后让您的标记处理程序类实现DynamicAttributes
接口:
And then have your tag handler class implement the DynamicAttributes
interface:
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
}
...
}
这篇关于jsp标记中的动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!