Is it possible to create custom attribute in jsp tag library without defining setter method?
I don't want to change the java code whenever there is addition of tag if the business logic will be remain same. I can access it by using Enumeration set of attributes(request.getAttributeNames()).
e.g.
<car:built
maker="ford" wheel="four" name="endeavor" engine="dohc" power="72bhp"
tyre="4" interior="mobile-charger,cigratte-lighter, music player"
/>
Rather than doing comma separated the interior, i wish to add in following way:
<car:built maker="ford" wheel="four" name="endeavor" engine="dohc" power="72bhp"
tyre="4" interior-mobile-charger="true" interior-cigratte-lighter="true"
interior-music-player-maker="JVC" 开发者_JAVA百科 interior-music player-bass="true"/>
So that, i can iterate and check whether attribute name contains "interior" word and store in a map and send it to the reports. It can also help me to define new attribute without changing the java code.
Is there any way to create custom attribute in jsp tag library without defining setter method?
What you're looking for is called "dynamic attributes".
See this previous question to see how to do it:
Problem in interpreting dynamic attributes in Jsp
Essentially, you add
<dynamic-attributes>true</dynamic-attributes>
To the .tld
descriptor, indicating that any old attributes can be passed to the tag. Your tag class must then implement the DynamicAttributes
interface, and the undeclared attributes will be passed in to your tag as a map:
For a tag to declare that it accepts dynamic attributes, it must implement this interface. The entry for the tag in the Tag Library Descriptor must also be configured to indicate dynamic attributes are accepted.
For any attribute that is not declared in the Tag Library Descriptor for this tag, instead of getting an error at translation time, the setDynamicAttribute() method is called, with the name and value of the attribute. It is the responsibility of the tag to remember the names and values of the dynamic attributes.
精彩评论