Code Snippets: Utilizing JSTL forEach tag in your Web Pages

JavaServer Pages Standard Tag Library or JSTL for short is a library that contains a set of core functionality including iterations and conditionals.  The tag I find myself most often using is the forEach tag to output a collection of data in a consistent format. By use of the optional status object and the if tag you can even put logic in that executes only if the item being iterated is first or last in the collection.

Getting Started

To utilize JSTL you first need to download the appropriate jar file and import it into you project in the web library. Once in your project you need to import the tag definition into your page using the following code.

<%@ taglib prefix="c" uri="http://java/sun.com/jsp/jstl/core %>

Basic ForEach Tag Usage

Once the tag library is imported we can use it by specifying 2 attributes var and items. VAR is used to denote the reference that the current item being iterated in the collection can be accessed by, while ITEMS specifies the collection to be iterated upon. Lets assume me have a collection of User objects that each have a firstName, lastName, and email property.  We could iterate over them with the following code.

<c:forEach var="user" items="${users}">
</c:forEach>

The above code is functional but doesn’t output any data lets expand upon it by having it export rows of a table with the three properties each in their own cell.

<table>
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Email</th>
  </tr>
  <c:forEach var="user" items="${users}">
    <tr>
      <td>${user.firstName}</td>
      <td>${user.lastName}</td>
      <td>${user.email}</td></tr>
  </c:forEach>
</table>

Advanced Usage: The varStatus Attribute

In addition to iteration over the collection the ForEach tag provides a status object that can be used to obtain information about where you are in the collection.

<c:forEach var "user" items="${users}" varStatus="status">
</c:forEach>
  • first and last property
    The status object contain a first and last property that returns a boolean set to true or false if the current item being iterated over is the first or last object in the collection. You can utilize the if tag to have code execute if it is one of these conditions.

    <c:if test="${status.last}">
      // code
    </c:if>
  • index property
    The index property gives you the current index of the object within the collection this value can be used to output different classes on odd and even rows for example, in addition to other uses.

    <table>
      <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
      </tr>
      <c:forEach var="user" items="${users}" varStatus="status">
        <tr class="${status.index % 2 == 0 ? 'even' : 'odd'}">
          <td>${user.firstName}</td>
          <td>${user.lastName}</td>
          <td>${user.email}</td>
        </tr>
      </c:forEach>
    </table>

Resources

// Code Snippets // JSP //

Comments & Questions

Add Your Comment