Spring MVC Tutorial
Core Spring MVC
Spring MVC - Annotation
Spring MVC - Form Handling
Spring MVC with JSTL
Spring MVC with REST API
Spring MVC with Database
The forEach
tag in JSTL is one of the most commonly used tags. It allows you to iterate over collections such as lists, arrays, and maps. The tag is part of the JSTL core library.
Let's set up a simple Spring MVC application that uses the forEach
tag to display a list of users.
Make sure you have the necessary dependencies:
<!-- Spring Web MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
Assume we have a simple User
model:
public class User { private String name; private String email; // Constructors, getters, setters, etc. }
Here's a controller that sends a list of users to a JSP view:
@Controller @RequestMapping("/users") public class UserController { @GetMapping("/list") public String listUsers(Model model) { List<User> users = Arrays.asList( new User("Alice", "alice@example.com"), new User("Bob", "bob@example.com"), new User("Charlie", "charlie@example.com") ); model.addAttribute("users", users); return "userList"; } }
forEach
In your userList.jsp
:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <title>Users List</title> </head> <body> <h2>List of Users:</h2> <table border="1"> <thead> <tr> <th>Name</th> <th>Email</th> </tr> </thead> <tbody> <c:forEach var="user" items="${users}"> <tr> <td>${user.name}</td> <td>${user.email}</td> </tr> </c:forEach> </tbody> </table> </body> </html>
In this JSP view, the forEach
tag iterates over the list of users
and for each user, it displays their name
and email
in a table row.
Run your Spring MVC application, navigate to /users/list
, and you should see a table displaying the list of users.
Remember, forEach
can also be used with arrays, maps, and even simple ranges using the begin
, end
, and step
attributes.
Spring MVC JSTL forEach Tag Example:
Description: This is a basic example showcasing the use of <c:forEach>
tag in a JSP page with JSTL in a Spring MVC application.
Code Snippet: (JSP Page)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!-- Other JSP content --> <c:forEach var="item" items="${itemList}"> <!-- Process each item in the list --> ${item} </c:forEach>
Using c:forEach in Spring MVC with JSTL:
Description: This example demonstrates using <c:forEach>
in a Spring MVC application with JSTL. It includes iterating over a collection and processing each element.
Code Snippet: (JSP Page)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!-- Other JSP content --> <c:forEach var="user" items="${userList}"> <!-- Process each user in the list --> ${user.username} </c:forEach>
Iterating Over a Collection with c:forEach in Spring MVC:
Description: This example focuses on using <c:forEach>
in Spring MVC to iterate over a collection and display its elements in a JSP page.
Code Snippet: (JSP Page)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!-- Other JSP content --> <c:forEach var="book" items="${bookList}"> <!-- Process each book in the list --> ${book.title} </c:forEach>
JSTL forEach Loop in Spring MVC Example:
Description: This example illustrates using JSTL's <c:forEach>
loop in a Spring MVC application to iterate over a collection and perform actions on each element.
Code Snippet: (JSP Page)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!-- Other JSP content --> <c:forEach var="item" items="${itemsList}"> <!-- Process each item in the list --> ${item.property} </c:forEach>
Displaying a List on JSP using c:forEach in Spring MVC:
Description: This example demonstrates using <c:forEach>
to display a list on a JSP page in a Spring MVC application.
Code Snippet: (JSP Page)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!-- Other JSP content --> <c:forEach var="item" items="${itemList}"> <!-- Display each item in the list --> ${item} </c:forEach>
How to Use c:forEach for Iteration in Spring MVC JSTL:
Description: This example provides guidance on how to use <c:forEach>
for iteration in a Spring MVC application with JSTL.
Code Snippet: (JSP Page)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!-- Other JSP content --> <c:forEach var="element" items="${elementList}"> <!-- Process each element in the list --> ${element.property} </c:forEach>
Spring MVC JSTL forEach Loop with Objects:
Description: This example demonstrates using <c:forEach>
in a Spring MVC application with JSTL to iterate over a collection of objects and display their properties.
Code Snippet: (JSP Page)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!-- Other JSP content --> <c:forEach var="employee" items="${employeeList}"> <!-- Display properties of each employee --> ${employee.name} </c:forEach>
Nested c:forEach in Spring MVC JSTL:
Description: This example illustrates the usage of nested <c:forEach>
loops in a Spring MVC application with JSTL. It includes iterating over a nested collection.
Code Snippet: (JSP Page)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!-- Other JSP content --> <c:forEach var="department" items="${departments}"> <!-- Display department details --> ${department.name} <c:forEach var="employee" items="${department.employees}"> <!-- Display employee details within the department --> ${employee.name} </c:forEach> </c:forEach>
Dynamic Iteration with c:forEach in Spring MVC:
Description: This example showcases dynamic iteration using <c:forEach>
in a Spring MVC application. It includes dynamically changing the iteration count based on a variable.
Code Snippet: (JSP Page)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!-- Other JSP content --> <c:forEach var="i" begin="1" end="${iterationCount}"> <!-- Process each iteration --> ${i} </c:forEach>
Using c:forEach for Iterating Through a Map in Spring MVC JSTL:
Description: This example demonstrates using <c:forEach>
to iterate through a map in a Spring MVC application with JSTL.
Code Snippet: (JSP Page)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!-- Other JSP content --> <c:forEach var="entry" items="${myMap}"> <!-- Display key and value of each map entry --> Key: ${entry.key}, Value: ${entry.value} </c:forEach>