Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
» JSP Standard Tag Library » JSTL Looping and Iteration Actions
JSTL Looping and Iteration Actions

One of the most common tasks you have to deal with in JSP is outputting a sets of data by using Java for and while loop. By doing so, you create a very unreadability JSP page with opening and closing curly brace. In addition, if something wrong happens, it is difficult to detect whether the problem is. Thankfully, JSTL provides you two useful actions for looping and iteration: for general data and for string of tokens.
The <c:forEach> action

The <c:forEach> action is very useful. You can loop over a collection or you can iterate number of times. There are two usages of <c:forEach> action. Let's take a look at the first one which you can use <c:forEach> for loop over a collection.

<c:forEach(var = "var"
        items="collection"
        varStatus="varStatusName">
<%-- processing of each item here --%>
</c:forEach>

The first two attributes are mandatory. You sepcify a collection in items attribute and each of item in collection in the var attribute. The varStatus attribute is optional. the varStatus attribute is an instance of class which implements interface LoopTagStatus. varStatus provides a set of useful properties to work with such as begin, end, current, index, count.... Let's take a look at an example:

<%@page contentType="text/html"
        pageEncoding="UTF-8"
        import="com.jsptutorial.*,java.util.*"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"
          prefix="c" %>

<html>
    <head>
        <title>Looping with c:forEach</title>
    </head>
    <body>
        <%
            ArrayList<Person> list = new ArrayList<Person>();
            Person p1 = new Person("Abel", "William");
            Person p2 = new Person("Sarah", "Palin");
            Person p3 = new Person("David", "Nguyen");
            list.add(p1);
            list.add(p2);
            list.add(p3);
            request.setAttribute("list", list);
        %>

        <table border="1">
            <thead>
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach var="person" items="${requestScope.list}">
                <tr>
                    <td><c:out value="${person.firstName}"  /></td>
                    <td><c:out value="${person.lastName}" /></td>
                </tr>
                 </c:forEach>
            </tbody>
        </table>
    </body>
</html>

In the code example above we create a list which contain three people and put that list object into the request object of the page. Then we loop over the list of people and print out their first name and last name by using <c:forEach> action.

If you want the table to have alternative row background color, you can use the varStatus to do so. Let's take a look at the modified version of the example above.

First we declare background color for odd and even rows by using CSS class.

<style type="text/css">
  .odd{ background-color:#fff}
  .even{background-color:#ccc}
</style>

Then we use varStatus attribute to access the LoopTagStatus object. Based on the current row of the iteration , we determine the odd and even row. Be noted that the current row of the iteration can be accessible via count property of the LoopTagStatus object.

<table border="1">
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
        </tr>
    </thead>
    <tbody>
    <c:forEach var="person"
               items="${requestScope.list}"
               varStatus ="row">

        <c:choose>
            <c:when test="${row.count % 2 == 0}">
                <c:set var="rowStyle" value="odd"  />
            </c:when>
            <c:otherwise>
                <c:set var="rowStyle" value="even"  />
            </c:otherwise>
        </c:choose>

        <tr class="${rowStyle}">
            <td><c:out value="${person.firstName}"  /></td>
            <td><c:out value="${person.lastName}" /></td>
        </tr>
    </c:forEach>
</tbody>
</table>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900