From e577b2cb78dcc219d69e02ad1b11e20deeb76850 Mon Sep 17 00:00:00 2001 From: Manfred <77407079+manfred106@users.noreply.github.com> Date: Sun, 9 Jul 2023 04:24:38 +0100 Subject: [PATCH] BAEL-6670: Reading a JSP variable from JavaScript (#14337) --- spring-boot-modules/spring-boot-jsp/pom.xml | 11 +++++++ .../jsp/controller/JSPVariableController.java | 31 +++++++++++++++++++ .../main/webapp/WEB-INF/jsp/jsp-var/by-el.jsp | 18 +++++++++++ .../webapp/WEB-INF/jsp/jsp-var/by-jsp.jsp | 16 ++++++++++ .../webapp/WEB-INF/jsp/jsp-var/by-jstl.jsp | 19 ++++++++++++ .../webapp/WEB-INF/jsp/jsp-var/to-dom.jsp | 19 ++++++++++++ 6 files changed, 114 insertions(+) create mode 100644 spring-boot-modules/spring-boot-jsp/src/main/java/com/baeldung/boot/jsp/controller/JSPVariableController.java create mode 100644 spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-el.jsp create mode 100644 spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-jsp.jsp create mode 100644 spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-jstl.jsp create mode 100644 spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/to-dom.jsp diff --git a/spring-boot-modules/spring-boot-jsp/pom.xml b/spring-boot-modules/spring-boot-jsp/pom.xml index ab81d65cc6..9c6d0fcc0b 100644 --- a/spring-boot-modules/spring-boot-jsp/pom.xml +++ b/spring-boot-modules/spring-boot-jsp/pom.xml @@ -46,12 +46,22 @@ jstl ${jstl.version} + + org.apache.commons + commons-text + ${commons-text.version} + org.apache.tomcat.embed tomcat-embed-jasper + + + org.springframework.boot + spring-boot-devtools + org.projectlombok lombok @@ -100,6 +110,7 @@ 1.2 2.4.4 2.17.1 + 1.10.0 \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-jsp/src/main/java/com/baeldung/boot/jsp/controller/JSPVariableController.java b/spring-boot-modules/spring-boot-jsp/src/main/java/com/baeldung/boot/jsp/controller/JSPVariableController.java new file mode 100644 index 0000000000..c85d588c07 --- /dev/null +++ b/spring-boot-modules/spring-boot-jsp/src/main/java/com/baeldung/boot/jsp/controller/JSPVariableController.java @@ -0,0 +1,31 @@ +package com.baeldung.boot.jsp.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("/jsp-var") +public class JSPVariableController { + + @GetMapping("/by-jsp") + public String byJsp() { + return "/jsp-var/by-jsp"; + } + + @GetMapping("/by-el") + public String byEl() { + return "/jsp-var/by-el"; + } + + @GetMapping("/by-jstl") + public String byJstl() { + return "/jsp-var/by-jstl"; + } + + @GetMapping("/to-dom") + public String byDom() { + return "/jsp-var/to-dom"; + } + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-el.jsp b/spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-el.jsp new file mode 100644 index 0000000000..381df4f5eb --- /dev/null +++ b/spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-el.jsp @@ -0,0 +1,18 @@ +<%@ page import="org.apache.commons.text.StringEscapeUtils" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<% + String jspMsg = StringEscapeUtils.escapeEcmaScript("Hello! This is Sam's page."); + request.setAttribute("jspMsg", jspMsg); +%> + + + Conversion by JSP EL + + + +
Open the browser console to see the message.
+ + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-jsp.jsp b/spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-jsp.jsp new file mode 100644 index 0000000000..84c1f0db29 --- /dev/null +++ b/spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-jsp.jsp @@ -0,0 +1,16 @@ +<%@ page import="org.apache.commons.text.StringEscapeUtils" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<% + String jspMsg = StringEscapeUtils.escapeEcmaScript("Hello! This is Sam's page."); +%> + + + Conversion by JSP expression tag + var jsMsg = '<%=jspMsg%>'; + console.info(jsMsg); + + + +
Open the browser console to see the message.
+ + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-jstl.jsp b/spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-jstl.jsp new file mode 100644 index 0000000000..5ac4bc8daa --- /dev/null +++ b/spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-jstl.jsp @@ -0,0 +1,19 @@ +<%@ page import="org.apache.commons.text.StringEscapeUtils" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<% + String jspMsg = StringEscapeUtils.escapeEcmaScript("Hello! This is Sam's page."); + request.setAttribute("scopedMsg", jspMsg); +%> + + + Conversion by JSTL + + + +
Open the browser console to see the message.
+ + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/to-dom.jsp b/spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/to-dom.jsp new file mode 100644 index 0000000000..2f44e5e664 --- /dev/null +++ b/spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/to-dom.jsp @@ -0,0 +1,19 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<% + String jspTag = "

Hello

"; +%> + + + Convert to an HTML tag + + + +
<%=jspTag%>
+
Open the browser console to see the tags.
+ + \ No newline at end of file