JSTL for Loop <c: forEach>
Consider a case where we want to do something with a
collection like an array, retrieving one element at a time, and prints that
element in a dynamically generated row. For this <c: forEach>
tag is perfect, this tag gives you the options to iterate over arrays
and collections.
In the tag <c:forEach var="i" begin="1" end="20" step="1" varStatus ="status">.
Here this tag is working exactly as, for loop works in a jsp or in java. In this
example i is the
name of the variable, which begins from 1 and continues up to 20, we are incrementing
the variable by 1, to increment the variable we have used step attribute. To print the value
of the variable on the browser use <c:out>
tag.
The code of the program is given below:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<title>JSTL Simple Conditional Execution Example</title>
</head>
<body bgcolor="#FFFFcc">
<h1>For Loop in JSTL</h1>
<c:forEach var="i" begin="1" end="20" step="1" varStatus ="status">
<c:out value="${i}" />
</c:forEach>
</body>
</html> |
The output of the program is given below:

Download this example.

|
Current Comments
1 comments so far (post your own) View All Comments Latest 10 Comments:In the foreach tag how to solve the following problem and what is the exact behaviour of the below code.
If <c:forEach var="i" varStatus ="status">
<c:if test="${status.index == 0}">this is true</c:if>
</c:foreach>
This is the problem we are developing in our own iterator tag.In the above code the functionality is not working if possible any one can explain and send how to solve this problem.If possible send some snnipet of code.
Posted by prasad on Sunday, 08.24.08 @ 21:47pm | #74844