问题
I want to define a jsp tag, which can accept some extra attributes which are not declared in the .tld file. Is it possible?
I want this because I want to define a <cms:img>
tag, which will generate a html img
tag. User can pass some attributes to it, but I don't want to limit user, I hope they can use it just as a normal html img tag.
For example, user can use this tag and pass some required information:
<cms:img id="111" />
Which will generate a html img tag as:
<img src="/show_images?id=111" />
All I need is a id
attribute.
But user may treat it as a normal html img tag, and pass some extra attributes to it, like:
<cms:img id="111" width="100px" height="100px" style="..." more attributes .. />
I don't want to declare the other attributes in my tag, because there are too many, and user may have their custom attributes.
So I want to know: Can I just declare the id
attribute in the jsp tag, but let it accept all the other undeclared attributes?
回答1:
Yes, it's possible. These are called dynamic attributes. See http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html#wp89775:
dynamic-attributes (optional)
Indicates whether this tag supports additional attributes with dynamic names. The value identifies a scoped attribute in which to place a Map containing the names and values of the dynamic attributes passed during invocation of the tag. A translation error results if the value of the dynamic-attributes of a tag directive is equal to the value of a name-given of a variable directive or the value of a name attribute of an attribute directive.
来源:https://stackoverflow.com/questions/9289429/define-a-jsp-tag-which-can-accept-some-extra-attributes-which-are-not-declared-i