Skip to content

Commit 07cc066

Browse files
authored
[jsp-map] map in jsp (#18751)
1 parent d7af433 commit 07cc066

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
lines changed

spring-boot-modules/spring-boot-jsp/pom.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@
3535

3636
<dependencies>
3737
<dependency>
38-
<groupId>javax.servlet</groupId>
39-
<artifactId>jstl</artifactId>
38+
<groupId>jakarta.servlet.jsp.jstl</groupId>
39+
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
40+
<version>${jstl.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.glassfish.web</groupId>
44+
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
4045
<version>${jstl.version}</version>
4146
</dependency>
4247
<dependency>
@@ -101,7 +106,7 @@
101106
</build>
102107

103108
<properties>
104-
<jstl.version>1.2</jstl.version>
109+
<jstl.version>2.0.0</jstl.version>
105110
<spring-boot.version>3.2.2</spring-boot.version>
106111
<commons-text.version>1.10.0</commons-text.version>
107112
</properties>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.boot.jsp.controller;
2+
3+
import java.util.Map;
4+
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
10+
@Controller
11+
@RequestMapping("/map-demo")
12+
public class MapDemoController {
13+
14+
private final static Map<String, String> movies = Map.of(
15+
// @formatter:off
16+
"M-01", "No Country for Old Men",
17+
"M-02", "The Silence of the Lambs",
18+
"M-03", "Back to the Future",
19+
"M-04", "Gone with the Wind",
20+
"M-05", "The Girl with the Dragon Tattoo"
21+
// @formatter:on
22+
);
23+
24+
@GetMapping("/using-scriptlets")
25+
public String usingScriplets(Model model) {
26+
model.addAttribute("movieMap", movies);
27+
return "map-demo/using-scriptlets";
28+
}
29+
30+
@GetMapping("/using-jstl")
31+
public String usingJstl(Model model) {
32+
model.addAttribute("movieMap", movies);
33+
return "map-demo/using-jstl";
34+
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
3+
<html>
4+
<head>
5+
<title>Demo - Using Map in JSP (JSTL)</title>
6+
<style>
7+
table, th, td {
8+
border: 1px solid black;
9+
border-collapse: collapse;
10+
padding: 3px;
11+
}
12+
</style>
13+
</head>
14+
<body>
15+
<div>Movies in the Map Object (Using JSTL)</div>
16+
<br/>
17+
<table>
18+
<tr>
19+
<th>Code</th>
20+
<th>Movie Title</th>
21+
</tr>
22+
<c:forEach items="${movieMap}" var="entry">
23+
<tr>
24+
<td>
25+
${entry.key}
26+
</td>
27+
<td>
28+
${entry.value}
29+
</td>
30+
</tr>
31+
</c:forEach>
32+
</table>
33+
</body>
34+
</html>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<%@ page import="java.util.Map" %>
2+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
3+
<html>
4+
<head>
5+
<title>Demo - Using Map in JSP (Scriptlets)</title>
6+
<style>
7+
table, th, td {
8+
border: 1px solid black;
9+
border-collapse: collapse;
10+
padding: 3px;
11+
}
12+
</style>
13+
</head>
14+
<body>
15+
<div>Movies in the Map Object (Using JSP Scriptlets)</div>
16+
<br/>
17+
<% Map<String, String> movieMap = (Map<String, String>) request.getAttribute("movieMap");%>
18+
<table>
19+
<tr>
20+
<th>Code</th>
21+
<th>Movie Title</th>
22+
</tr>
23+
<%
24+
if (movieMap != null) {
25+
for (Map.Entry<String, String> entry : movieMap.entrySet()) {
26+
String key = entry.getKey();
27+
String value = entry.getValue();
28+
%>
29+
<tr>
30+
<td>
31+
<%= key %>
32+
</td>
33+
<td>
34+
<%= value %>
35+
</td>
36+
</tr>
37+
<% }
38+
}%>
39+
</table>
40+
</body>
41+
</html>

0 commit comments

Comments
 (0)