I want to use the actual item from my c:forEach in a <% JavaCode/JSPCode %>.
How do I access to this item?
<c:forEach var="item" items="${list}">
<% MyProduct p = (MyProduct) ${item}; %> <--- ???
</c:forEach>
Anything that goes inside <% %> has to be valid Java, and ${item} isn't. The ${...} is JSP EL syntax.
You can do it like this:
<c:forEach var="item" items="${list}">
<% MyProduct p = (MyProduct) pageContext.getAttribute("item"); %>
</c:forEach>
However, this is a horrible way to write JSPs. Why do you want to use scriptlets, when you're already using JSTL/EL? Obviously you're putting something inside that <forEach>, and whatever it is, you should be able to do without using a scriptlet.
Don't use scriptlets (ie. the stuff in between the <% %> tags. It's considered bad practice because it encourages putting too much code, even business logic, in a view context. Instead, stick to JSTL and EL expressions exclusively. Try this:
<c:forEach var="item" items="${list}">
<c:set var="p" value="${item}" />
</c:forEach>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With