Open In App

JSTL Core <c:forEach> Tag

Last Updated : 26 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The JSTL Core <c:forEach> tag is used to iterate over a collection of objects or a range of values. It is the most commonly used JSTL tag because it can be used to implement a variety of functionality.

Attributes of <c:forEach>

The <c:forEach> tag has the following attributes:

  • var: Variable to point to the current item in the collection.
  • items: Collection of objects or range of values to iterate over.
  • begin: Index of the first item to start the iteration.
  • end: Index of the first item to end the iteration.
  • step: number of items to skip in between the iterations.
  • varStatus: Variable to tell the loop status.

Usage of <c:forEach>

Firstly we have to declare a variable that points to the current item of the collection. Next, we have to tell the range of values to iterate or the collection of objects. In the end, we have the body of the loop that is performed for each item of the collection.

Examples for <c:forEach> Tag

Example 1:

HTML




<!-- Simple example demonstrating <c:forEach> tag -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  
<html>
<head>
<title>c:forEach Tag Example</title>
</head>
<body>
  
<c:forEach var="item" items="${items}">
    <h1>${item}</h1>
</c:forEach>
  
</body>
</html>


Note: Above code will display each item in <H1> tag, you can customise the presentation as per you need.

Example 2:

The <c:forEach> tag can be used to implement more complex functionality. For example, we use the begin and end attributes to iterate over a range of items. Also step can be used to skip between the ranges, default step size is 1.

In addition, we use varStatus to expose the loop status to the body of the loop. This can be useful for tasks such as checking whether the current iteration is the first or last iteration, or for displaying the current index of the loop.

HTML




<!-- Demonstration for whether varStatus attribute's current iteration is the last iteration -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  
<html>
<head>
<title>c:forEach Tag Example</title>
</head>
<body>
  
<c:forEach var="item" items="${items}">
  
    <c:if test="${varStatus.last}">
        This is the last item in the list.
    </c:if>
  
</c:forEach>
  
</body>
</html>


This code will iterate over the items collection and display the message “This is the last item in the list.” only for the last item in the list.

Example 3:

We an also use the range based iteration, for that we use begin, end to denote the range and var to name the variable which will iterate through the range.

HTML




<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  
<html>
  <head>
      <title>c:forEach Tag Example</title>
  </head>
<body>
  
  <c:forEach var="i" begin="400" end="404">  
    Code <c:out value="${i}"/>
  </c:forEach>
  
</body>
</html>


Conclusion

The JSTL Core <c:forEach> tag is a iteration tag that can be used to implement variety of functionality. It is one of the most commonly used JSTL tags, and it is essential for learning JSTL.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads