Spring MVC - JSTL forEach Tag with Example

Last Updated : 13 Jun, 2026

The JSTL <c:forEach> tag is used to iterate over collections such as List, Array, Set, Map, or arrays in JSP pages. It eliminates the need for Java scriptlets and provides a clean way to display dynamic data in Spring MVC applications.

  • Iterates over collections and arrays.
  • Reduces Java code inside JSP pages.
  • Improves readability and maintainability.
  • Works seamlessly with Spring MVC model attributes.

Syntax of JSTL forEach Tag

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

  • Iterates through each element of a collection, list, array, or map.
  • The current element is stored in the variable specified by var during each iteration.

Syntax with begin, end and step

<c:forEach var="num" begin="1" end="10" step="2">
${num}
</c:forEach>

  • Iterates over a specified range of values from begin to end.
  • The step attribute controls the increment value between iterations.

When to Use JSTL forEach?

  • Displaying database records.
  • Showing product lists.
  • Displaying employee/student data.
  • Iterating arrays received from forms.

Attributes of <c:forEach> Tag

AttributeDescription
varVariable name used during iteration
itemsCollection or array to iterate
beginStarting index
endEnding index
stepIncrement value
varStatusProvides iteration status information

Step-by-Step Implementation to use JSTL forEach Tag in Spring MVC

Follow these steps to configure JSTL forEach tag in Spring Mvc Applications.

Step 1: Create a Maven Project

  • Open STS IDE.
  • Click File - New - Maven Project.
  • Select Create a simple project (Select archetype ) and click Next.

Then Enter the following details:

  • Group Id: com.gfg
  • Artifact Id: SpringMVCMultipleViewPage
  • Packaging: war

Click Finish.

Step 2: Add Required Dependencies

Add the following maven dependencies and plugin to your pom.xml file.

XML
<project xmlns="https://maven.apache.org/POM/4.0.0"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.geeksforgeeks</groupId>
    <artifactId>simple-calculator</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>simple-calculator Maven Webapp</name>
    <url>http://maven.apache.org</url>
  
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
      
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.18</version>
        </dependency>
      
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/jstl/jstl -->
         <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
         </dependency>
    </dependencies>
  
    <build>
        <finalName>simple-calculator</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Before moving into the coding part let's have a look at the file structure in the below image. 

Step 3: Configure Dispatcher Servlet

Registers DispatcherServlet and loads Spring MVC configuration classes automatically.

Java
package com.geeksforgeeks.calculator.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class CalculatorAppIntilizer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    // Registering the Spring config file
    @Override
    protected Class<?>[] getServletConfigClasses() {
        Class aClass[] = { CalculatorAppConfig.class };
        return aClass;
    }

    // Add mapping url
    @Override
    protected String[] getServletMappings() {
        String arr[] = { "/geeksforgeeks.org/*" };
        return arr;
    }

}

Step 4: Configure Spring MVC

Enables Spring MVC features, component scanning, and configures ViewResolver for JSP pages.

Java
package com.geeksforgeeks.calculator.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.geeksforgeeks.calculator.controllers")
public class CalculatorAppConfig {

    // setup ViewResolver
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}

Step 5: Create Controller Class

Creates a list of skills, stores it in ModelAndView, and sends it to the JSP page.

Java
package com.geeksforgeeks.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 Sceince");
        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;

    }

}

Step 6: Create JSP Page

Enables JSTL core tags such as <c:forEach>, <c:if>, and <c:choose>. it Iterates through each element of the skillList collection and displays it as a list item.

HTML
<%@ taglib uri="http://www.oracle.com/technetwork/java/index.html" 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>

Step 7: Run the Application

  • Right-click the project.
  • Select Run As - Run on Server.
  • Choose Apache Tomcat Server.
  • Click Finish.

After that use the following URL to run your controller

http://localhost:8080/simple-calculator/geeksforgeeks.org/jstldemo

Output:

So on the output screen, you can see both the list with and without Iteration.

Comment