Passing dynamic attributes between JSP tags?

I have a set of JSP tags that use dynamic-attributes to allow for arbitrary HTML attributes to be passed in to them, like this:

<%-- tag named a:div --%>
<%@ tag dynamic-attributes="attrs" %>
<div <c:forEach var="attr" items="${attrs}"> ${attr.key}="${attr.value}"</c:foreach>>
</div>

I then have another tag that I want to be able to pass dynamic-attributes to & have it just pass them onto the above tag. Something like:

<%-- tag using a:div --%>
<%@ tag dynamic-attributes="attrs" %>
<a:div class='big' attrs="${attrs}"/>

But attrs=”${attrs}” doesn’t do what I want. Any ideas?

Answer

I don’t think this is possible, sadly. The “attrs” object here is a java.util.Map, and there’s no way to explode that back.

The alternative is to allow your <a:div> tag to take a Map attribute which represents those dynamic attributes. Perhaps <a:div> could support both dynamic attributes as well as having a Map passed in from somewhere else, and treat them the same using <c:choose> or the like?

Leave a Reply

Your email address will not be published. Required fields are marked *