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.
Contact Us