I have a custom jsp tag that outputs a spring form input field (plus other layout elements). What I need to do is to be able to accept dynamic attributes and use them in the input field as-is.
Eg of use:
<mytag:myinputtag arbitraryAttribute="value"/>
Should output
<input:form arbitraryAttribute="value" />
Unfortunately it doesn't work as expected because it throws an unterminated form:input tag exception. Following is the code I used:
<%@ tag dynamic-attributes="attributes" %>
<c:set var="expandedAttributes">
<c:forEach var="a" items="${ attributes }">
${a.key}="${a.value}"<%= " " %>
</c:forEach>
</c:set>
<form:input (...) ${expandedAttributes} />
I can understand why this doesn't work as expected because of the resolution order of the EL expr and the tags. Therefore I have also tested to inject directly using scriptlets
<form:input (...) <%= (String)jspContext.getAttribute("expandedAttributes")%> --%>
So I need a solution to this issue as I cannot preview all the attributes that could be added to the input. Therefore I thought of the following possibilities:
- Using
<input
instead of<form:input
, meaning that I have to replicate exactly the spring code for the "path" attribute (not good idea) - Extend form:input tag, copy dynamic attributes into default attributes and do standard tag rendering (don't know if is a feasible solution and if spring change its input tag implementation then it could not work properly anymore)
- Enumerating all the attributes I need, this make the tag code huge and less maintainable
I would like to know if maybe there is a better solution I haven't though of, or if the second possibility is feasible at all.
Thanks