forEach in JSP Core Tag Library
How is forEach from JSP Core Tag Library used?
Explanation
Like any other looping statement the
forEach tag of the Core tag library can be used to iterate over the given values or objects.
Syntax:
<c:forEach var="name" items="expression"
begin="expression" end="expression" step="expression">
body
</c:forEach>
In the above syntax the "items" attribute can be used to specify a collection of items, the attribute "var" contains the current item. With the given "begin", "end" values the iteration takes place as per the value given in "steps". So many collection types are available with java.util.Collection and java.util.Map
Example :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<p>for Each loop Example</p>
<c:forEach var="a" begin="1" end="4" step="1"
varStatus ="status">
<c:out value="${a*2}" />">
</c:forEach>
Result :
for Each loop Example
2 4 6 8
In the above example we use the "<c:forEach" loop to display the numbers multiplied by "2" in individual textboxes.