1. Overview
When working with JavaServer Pages (JSP), we often simplify our code using the JavaServer Pages Standard Tag Library (JSTL).
One of the most commonly used JSTL tags is <c:forEach>, which allows us to iterate over collections such as arrays, lists, or maps. While iterating, we frequently need to access the index of each element.
In this quick tutorial, weâll explore getting the elementsâ index values while iterating using the JSTL <c:forEach> tag.
2. Creating a Simple Example
As an example, weâll create a simple Spring-MVC JSTL web application to list some predefined movies. For simplicity, weâll skip Spring configurations such as Spring ViewResolver.
First, letâs look at the Movie POJO:
public class Movie {
private String title;
private int year;
public Movie(String title, int year) {
this.title = title;
this.year = year;
}
//... getter and setter methods are omitted
}
Then, we need a Spring Controller class to process HTTP requests. So, letâs create one:
@Controller
public class JSTLForEachDemoController {
@RequestMapping(value = "/foreach-demo", method = RequestMethod.GET)
public ModelAndView forEachDemo(final Model model) {
ModelAndView mv = new ModelAndView("jstlForEachDemo");
List<Movie> movies = List.of(
new Movie("The Hurt Locker", 2008),
new Movie("A Beautiful Mind", 2001),
new Movie("The Silence of the Lambs", 1991),
new Movie("A Man for All Seasons", 1966),
new Movie("No Country for Old Men", 2007)
);
mv.addObject("movieList", movies);
return mv;
}
}
As we can see, the JSTLForEachDemoController class has only one method to handle the â/foreach-demoâ GET request. For simplicity, we prepared a List of hard-coded Movie objects and added it to the ModelAndView object named âmovieListâ. Further, we defined jstlForEachDemo as the view to present the Movie data.
Finally, letâs create a JSP file as the view at webapp/WEB-INF/views/jstlForEachDemo.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
<head>
<title>JSTL ForEach Example</title>
</head>
<body>
<h1>Movie List</h1>
<div>
<table style="width: 50%">
<tr>
<th>Year</th>
<th>Title</th>
</tr>
<c:forEach var="movie" items="${movieList}">
<tr>
<td>${movie.year}</td>
<td>${movie.title}</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
As the code example shows, we used JSTLâs <c:forEach> tag to loop through the movieList object and generate the rows in the HTML table.
If we deploy our web application to a servlet container, such as Apache Tomcat, we can see the moviesâ data by accessing the URL in a web browser:
Next, letâs figure out how to obtain each Movie elementâs index during the forEach iteration.
3. Using varStatus to Access Index and Count
Fortunately, JSTL provides a built-in mechanism for getting the indexes of elements in a forEach loop. The varStatus attribute allows us to define a loop status variable that provides useful metadata about the iteration.Â
Next, letâs add the varStatus attribute to the <c:forEach> tag and show the element indexes in the HTML table:
...
<h1>Movie List</h1>
<div>
<table style="width: 50%">
<tr>
<th>varStatus.index</th>
<th>Year</th>
<th>Title</th>
</tr>
<c:forEach var="movie" items="${movieList}" varStatus="theLoop">
<tr>
<td>${theLoop.index}</td>
<td>${movie.year}</td>
<td>${movie.title}</td>
</tr>
</c:forEach>
</table>
</div>
...
In this example, we named varStatus in the forEach loop âtheLoopâ and obtained an elementâs index by accessing varStatusâs index property: theLoop.index.
Now, if we access the same URL, we can see the new column containing the indexes of the elements in movieList:
As the screenshot shows, varStatusâs index is zero-based. However, sometimes, we want to have one-based indexes in our view. Of course, we can quickly achieve it by adding one to varStatusâs index, for example, ${theLoop.index + 1}. In fact, varStatus offers the count property, which provides one-based indexes.
Next, letâs add another one-based index column next to the zero-based one, using varStatus.count:
...
<h1>Movie List</h1>
<div>
<table style="width: 50%">
<tr>
<th>varStatus.index</th>
<th>varStatus.count</th>
<th>Year</th>
<th>Title</th>
</tr>
<c:forEach var="movie" items="${movieList}" varStatus="theLoop">
<tr>
<td>${theLoop.index}</td>
<td>${theLoop.count}</td>
<td>${movie.year}</td>
<td>${movie.title}</td>
</tr>
</c:forEach>
</table>
</div>
...
Letâs redeploy our application and check the result in the browser:
The table includes one-based indexes, as expected.
4. Other Useful Properties of varStatus
Using the built-in varStatus attribute in the JSTLâs <c:forEach> tag, weâve successfully obtained element indexes. Apart from index and count, varStatus offers other valuable properties.
Sometimes, in a loop, we need to detect whether the current element is the first or the last. For example, we want to set different background colors for the first and last movie in our HTML table.
varStatus provides two boolean properties, first and last, which allow us to easily detect if weâre in the first or the last iteration, respectively.
Next, letâs set different background colors on the first and last movie rows by checking varStatusâs first and last properties:
...
<style>
...
.first { background-color: lightgreen }
.last { background-color: orange }
</style>
...
<h1>Movie List</h1>
<div>
<table style="width: 50%">
<tr>
<th>varStatus.index</th>
<th>varStatus.count</th>
<th>Year</th>
<th>Title</th>
</tr>
<c:forEach var="movie" items="${movieList}" varStatus="theLoop">
<tr ${ theLoop.first ? 'class="first"' : '' } ${ theLoop.last ? 'class="last"' : '' }>
<td>${theLoop.index}</td>
<td>${theLoop.count}</td>
<td>${movie.year}</td>
<td>${movie.title}</td>
</tr>
</c:forEach>
</table>
</div>
...
We defined two CSS classes for the background colors. Then, in the <tr> tag, we set the corresponding class name by checking varStatusâs first and last properties.
With this change, letâs see the result in the browser:
Now, the first row has a light green background, and the last rowâs background is orange.
5. Conclusion
In this article, weâve explored how to access an index within a <c:forEach> loop in JSP with JSTL. Whether we need zero-based indexing for logic or one-based counting for display purposes, JSTL provides an easy-to-use solution for handling loops effectively.
In addition, we demonstrated detecting the first and the last iteration in a <c:forEach> loop using varStatusâs first and last properties.
The code backing this article is available on GitHub. Once you're
logged in as a Baeldung Pro Member, start learning and coding on the project.