Skip to content

Commit 6d0668c

Browse files
authored
[JSTL-foreach] varStatus in JSTL for-each (#18425)
1 parent 6132cbb commit 6d0668c

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

spring-web-modules/spring-mvc-forms-jsp/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@
3535
<artifactId>jakarta.servlet.jsp-api</artifactId>
3636
<version>${jakarta.servlet.jsp-api.version}</version>
3737
</dependency>
38+
<dependency>
39+
<groupId>jakarta.servlet.jsp.jstl</groupId>
40+
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.glassfish.web</groupId>
44+
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
45+
</dependency>
3846
<dependency>
3947
<groupId>com.mysql</groupId>
4048
<artifactId>mysql-connector-j</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.jstl.foreachdemo;
2+
3+
import java.util.List;
4+
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
import org.springframework.web.servlet.ModelAndView;
10+
11+
@Controller
12+
public class JSTLForEachDemoController {
13+
14+
@RequestMapping(value = "/foreach-demo", method = RequestMethod.GET)
15+
public ModelAndView forEachDemo(final Model model) {
16+
ModelAndView mv = new ModelAndView("jstlForEachDemo");
17+
18+
List<Movie> movies = List.of(
19+
//@formatter:off
20+
new Movie("The Hurt Locker", 2008),
21+
new Movie("A Beautiful Mind", 2001),
22+
new Movie("The Silence of the Lambs", 1991),
23+
new Movie("A Man for All Seasons", 1966),
24+
new Movie("No Country for Old Men", 2007)
25+
//@formatter:on
26+
);
27+
mv.addObject("movieList", movies);
28+
return mv;
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.jstl.foreachdemo;
2+
3+
public class Movie {
4+
5+
private String title;
6+
private int year;
7+
8+
public Movie(String title, int year) {
9+
this.title = title;
10+
this.year = year;
11+
}
12+
13+
public String getTitle() {
14+
return title;
15+
}
16+
17+
public void setTitle(String title) {
18+
this.title = title;
19+
}
20+
21+
public int getYear() {
22+
return year;
23+
}
24+
25+
public void setYear(int year) {
26+
this.year = year;
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3+
<html>
4+
<style>
5+
table, th, td {
6+
border: 1px solid black;
7+
border-collapse: collapse;
8+
}
9+
.first { background-color: lightgreen }
10+
.last { background-color: orange }
11+
</style>
12+
<head>
13+
<title>JSTL ForEach Example</title>
14+
</head>
15+
<body>
16+
<h1>Movie List</h1>
17+
<div>
18+
<table style="width: 50%">
19+
<tr>
20+
<th>varStatus.index</th>
21+
<th>varStatus.count</th>
22+
<th>Year</th>
23+
<th>Title</th>
24+
</tr>
25+
<c:forEach var="movie" items="${movieList}" varStatus="theLoop">
26+
<tr ${theLoop.first ? 'class="first"' : ''} ${theLoop.last ? 'class="last"' : ''}>
27+
<td>${theLoop.index}</td>
28+
<td>${theLoop.count}</td>
29+
<td>${movie.year}</td>
30+
<td>${movie.title}</td>
31+
</tr>
32+
</c:forEach>
33+
</table>
34+
</div>
35+
</body>
36+
</html>

0 commit comments

Comments
 (0)