Create Controller

Go to the src/main/java folder and inside this folder create a class named Jstldemocontroller and put it inside the com.w3wiki.calculator.controllers package. Below is the code for the Jstldemocontroller.java file. In this file, we are going to create the ArrayList

File: Jstldemocontroller.java file

Java




package com.w3wiki.calculator.controllers;
 
import java.util.ArrayList;
import java.util.List;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class Jstldemocontroller {
 
    @RequestMapping("/jstldemo")
    public ModelAndView showHomePage() {
 
        // Create the List of Skills
        List<String> skills = new ArrayList<String>();
        skills.add("Data Science");
        skills.add("Data Structure");
        skills.add("Javascript");
        skills.add("Python");
        skills.add("SQL");
 
        // Using ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("jstl-demo-page");
        modelAndView.addObject("skillList", skills);
 
        return modelAndView;
 
    }
 
}


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