Access JSTL tag from code inside forEach loop

Is it possible to access the JSTL forEach variable through code from a loop?

<c:forEach items="${elements}" var="element">
    <% element.someMethod(); %>
</c:forEach>
+5
source share
2 answers

Well, I believe that the "element" is stored in the context of the page.

<c:forEach items="${elements}" var="element">
    <% ((Element) pageContext.getAttribute("elements")).someMethod(); %>
</c:forEach>
+3
source

Change after correction example:

Yes you can access varinsidec:forEach

Here is an example:

<c:forEach items="${elements}" var="element">
    ${((Element)element).someMethod()}
</c:forEach>

See c: forEach in the JSTL documentation.

-1
source

All Articles