Search code examples
javajsptagsdynamic-attributes

How Do I make dynamic-attributes Work in JSP Tag Files?


So according to my JSP reference book, as well as every other reference I can find on the web, I'm supposed to be able to do something like:

<%@ tag dynamic-attributes="dynamicAttributesVar" %>

and then when someone uses an attribute that I didn't define in an attribute directive, I should be able to access that attribute from the "dynamicAttributesVar" map:

<%= dynamicAttributesVar.get("someUnexpectedAttribute") %>

However, that doesn't work, at all; I just get a "dynamicAttributesVar cannot be resolved" error when I try.

Now, I did discover (by looking at the generated Java class for the tag) that I can "hack" a working dynamic attributes variable by doing:

<% Map dynamicAttributesVar = _jspx_dynamic_attrs; %>

Now, that hack doesn't work unless I also use the dynamic-attributes parameter on my tag directive, so it seems that the parameter is doing something.

But what I want to know is, how can I make it do what it does for every other JSP user out there?


Solution

  • Isn't "dynamicAttributesVar" the name of the key in the page context that the dynamic attributes are put into? So you could do

    <c:out value="${dynamicAttributesVar.someUnexpectedAttributes}"/>
    

    or if you must use scriptlets:

    Map dynamicAttributes = (Map) pageContext.getAttribute("dynamicAttributesVar")
    

    (Disclaimer: I haven't tried it, I've just used dynamic attributes in tags with direct Java implementations... but it seems reasonable)