Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions spring-boot-modules/spring-boot-jsp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
Expand Down Expand Up @@ -101,7 +106,7 @@
</build>

<properties>
<jstl.version>1.2</jstl.version>
<jstl.version>2.0.0</jstl.version>
<spring-boot.version>3.2.2</spring-boot.version>
<commons-text.version>1.10.0</commons-text.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.baeldung.boot.jsp.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/map-demo")
public class MapDemoController {

private final static Map<String, String> movies = Map.of(
// @formatter:off
"M-01", "No Country for Old Men",
"M-02", "The Silence of the Lambs",
"M-03", "Back to the Future",
"M-04", "Gone with the Wind",
"M-05", "The Girl with the Dragon Tattoo"
// @formatter:on
);

@GetMapping("/using-scriptlets")
public String usingScriplets(Model model) {
model.addAttribute("movieMap", movies);
return "map-demo/using-scriptlets";
}

@GetMapping("/using-jstl")
public String usingJstl(Model model) {
model.addAttribute("movieMap", movies);
return "map-demo/using-jstl";

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Demo - Using Map in JSP (JSTL)</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 3px;
}
</style>
</head>
<body>
<div>Movies in the Map Object (Using JSTL)</div>
<br/>
<table>
<tr>
<th>Code</th>
<th>Movie Title</th>
</tr>
<c:forEach items="${movieMap}" var="entry">
<tr>
<td>
${entry.key}
</td>
<td>
${entry.value}
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<%@ page import="java.util.Map" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Demo - Using Map in JSP (Scriptlets)</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 3px;
}
</style>
</head>
<body>
<div>Movies in the Map Object (Using JSP Scriptlets)</div>
<br/>
<% Map<String, String> movieMap = (Map<String, String>) request.getAttribute("movieMap");%>
<table>
<tr>
<th>Code</th>
<th>Movie Title</th>
</tr>
<%
if (movieMap != null) {
for (Map.Entry<String, String> entry : movieMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
%>
<tr>
<td>
<%= key %>
</td>
<td>
<%= value %>
</td>
</tr>
<% }
}%>
</table>
</body>
</html>