Dynamic attributes in jsp tag

I want to have a tag with dynamic attributes, such as simple html tags , for example .

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

And in my tag implementation, 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 note that these dynamic attributes will be written by hand in jsp, and not just as a Map, for example ${someMap} . So is there a way to achieve this?

+7
java jsp
source share
1 answer

You need to enable dynamic attributes in your TLD, for example:

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

And then your tag handler class implements 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 } ... } 
+4
source share

All Articles