Create JSP Page and Iterate List using JSTL

Now we have to create a view named “registration-page” inside the WEB-INF/view folder with the .jsp extension. So go to the src > main > webapp > WEB-INF and create a folder view and inside that folder create a jsp file named jstl-demo-page. Before using JSTL we have to make the following changes to our project. 

Step 1: Add the below dependency to your pom.xml file. 

XML




<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>


Step 2: Add the JSTL tags in the JSP files (Here  jstl-demo-page.jsp file).

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

Step 3: Write down the following for loop to iterate the Array.

<c:forEach var="skill" items="${skillList}">
            <li>${skill}</li>
</c:forEach>

Below is the complete code for the jstl-demo-page.jsp file. 

File: jstl-demo-page.jsp

HTML




<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
</head>
<body>
    <h3>List Without Iteration :</h3>
    ${skillList}
    <h3>List With Iteration :</h3>
    <ul>
 
        <c:forEach var="skill" items="${skillList}">
            <li>${skill}</li>
        </c:forEach>
 
    </ul>
</body>
</html>


So now we have done with the coding part. Let’s run and test our application. 

Spring MVC – Iterating List on JSP using JSTL

JSP Standard Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. JSTL aims to provide an easy way to maintain SP pages The use of tags defined in JSTL has Simplified the task of the designers to create Web pages. They can now simply use a tag related to the task that they need to implement on a JSP page.

To read more in-depth about JSTL refer to this article: JSP Standard Tag Library

To iterate a List on JSP, the <c:forEach> is a commonly used. 

Syntax:

For example, ${list} points to a List<Object>, then the following

<c:forEach var="item" items="${list}" >
    ${item}
</c:forEach>

does basically the same as following in “core Java”:

for (Object item : list) {
    System.out.println(item);
}

In this article, we are going to discuss how can we iterate the List on JSP using JSTL in detail with an example project in Spring MVC. 

Example Project

Similar Reads

Setup the Project

We are going to use Spring Tool Suite 4 IDE for this project. Please refer to this article to install STS on your local machine How to Download and Install Spring Tool Suite (Spring Tools 4 for Eclipse) IDE? Go to your STS IDE then create a new maven project, File > New > Maven Project, and choose the following archetype as shown in the below image as follows:...

Setup ViewResolver

...

Create Controller

...

Create JSP Page and Iterate List using JSTL

...

Run Your Application

Spring MVC is a Web MVC Framework for building web applications. In generic all MVC frameworks provide a way of working with views. Spring does that via the ViewResolvers, which enables you to render models in the browser without tying the implementation to specific view technology. Read more here: ViewResolver in Spring MVC. So for setting up ViewResolver go to the CalculatorAppConfig.java file and write down the code as follows...

Contact Us