For other reasons I have to do a forEach loop on a jsp page rather than iterating over a collection. I need to display the value of a collection, but I am returning a syntax error because of the inner braces..do I have to escape it first? How do I get it to resolve the value of i then to get the value from the colllection?
<c:if test="${maxColumns >= 0}">
<c:forEach var="i" begin="0" end="${maxColumns}" step="1" varStatus ="status">
<td height="20"> ${columnNames[${i}].columnName</td>
</c:forEach>
</c:if>
Thanks for the help.
You cannot nest JSP EL expressions like that, but you don’t have to–the correct expression is:
iis already part of the EL scope because of theforEachtag, and should be evaluated as any other scoped variable, likecolumnNames. Here it’s just a collection index.While I’m sympathetic to the perceived need to do this kind of logic in the view layer, IMO it’s almost always a better idea to prep collection ranges in Java–it’s also easier to test.