I have a JSP that needs to print some text that is created using a loop iterator and feed it to another object (Spring bean), something like:
<c:forEach var="myVar" items="${myVars}"> <c:out value="anotherObject.getFoo(myVar)"/> </c:forEach>
Obviously, the above code is invalid because the JSTL statement . allows only calls without parameters. I see the following solutions to the problem:
1) Scripts
<c:forEach var="myVar" items="${myVars}"> <% SomeType myVar = (SomeType) pageContext.getAttribute("myVar"); SomeOtherType anotherObject = (SomeOtherType) pageContext.getAttribute("anotherObject"); YetAnotherType result = anotherObject.getFoo(myVar); pageContext.setAttribute("result", result); %> <c:out value="${result}"/> </c:forEach>
The obvious point here is JSP code pollution and general ugliness.
2) Writing a tag that does everything that is done inside the scriptlets. A typical example of over engineering, yuck!
3) Expand the myVars collection and replace each myVar with a dynamic proxy whose InvocationHandler will add an extra-less parameter to make all getFoo() calls through anotherObject . All this will be done in the controller, so the JSP will remain clean and myVar will remain the same. But at what price?
I cannot add the .getFoo() method to myVar because it does not fit and breaks the separation of problems.
It seems that the transfer parameters will be possible in JSP / EL 2.2, but I am using Tomcat 6.0.29, which only binds the EL 2.1 API.
Question: can anyone suggest the cleanest approach for this situation?
mindas
source share