How to make dynamic attributes work in JSP tag files?

So, according to my JSP reference, as well as any other link I can find on the Internet, I should have done something like:

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

and then, when someone uses an attribute that I did not define in the attribute directive, I should have access to this attribute from the dynamicAttributesVar map:

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

However, this does not work; I just get the error "dynamicAttributesVar could not be resolved" when I try.

Now I discovered (by looking at the generated Java class for the tag) that I can “crack” the working dynamic attribute variable by doing:

<% Map dynamicAttributesVar = _jspx_dynamic_attrs; %>

Now this hack does not work unless I also use the dynamic-attributes parameter in my tag directive, so it seems that the parameter is doing something.

, , JSP?

+3
2

"dynamicAttributesVar" , ? ,

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

:

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

( : , Java... )

+4

.

, O'Reilly, JSTL .

/:

<c:forEach items="${dynamicAttributesVar}" var="a"> 
${a.key}="${a.value}" 
</c:forEach> 

:

<c:out value="${dynamicAttributesVar['someUnexpectedAttribute']}"/>
+5

Source: https://habr.com/ru/post/1706626/


All Articles