Skip to content

Commit

Permalink
BAEL-6670: Reading a JSP variable from JavaScript (eugenp#14337)
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred106 committed Jul 9, 2023
1 parent 4746fdc commit e577b2c
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
11 changes: 11 additions & 0 deletions spring-boot-modules/spring-boot-jsp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,22 @@
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!--uncomment below if deploying in web container -->
<!--<scope>provided</scope> -->
</dependency>
<!-- devtools enables code hotswap in embedded server -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down Expand Up @@ -100,6 +110,7 @@
<jstl.version>1.2</jstl.version>
<spring-boot.version>2.4.4</spring-boot.version>
<log4j2.version>2.17.1</log4j2.version>
<commons-text.version>1.10.0</commons-text.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -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";
}

}
Original file line number Diff line number Diff line change
@@ -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);
%>
<html>
<head>
<title>Conversion by JSP EL</title>
<script type="text/javascript">
var jsMsg = '${requestScope.jspMsg}';
console.info(jsMsg);
</script>
</head>
<body>
<div>Open the browser console to see the message.</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -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.");
%>
<html>
<head>
<title>Conversion by JSP expression tag</title>
var jsMsg = '<%=jspMsg%>';
console.info(jsMsg);
</script>
</head>
<body>
<div>Open the browser console to see the message.</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -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);
%>
<html>
<head>
<title>Conversion by JSTL</title>
<script type="text/javascript">
var jsMsg = '<c:out value="${scopedMsg}" escapeXml="false"/>';
console.info(jsMsg);
</script>
</head>
<body>
<div>Open the browser console to see the message.</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String jspTag = "<h1>Hello</h1>";
%>
<html>
<head>
<title>Convert to an HTML tag</title>
<script type="text/javascript">
function printTag() {
var tags = document.getElementById("fromJspTag").innerHTML;
console.info(tags);
}
</script>
</head>
<body onLoad="printTag()">
<div id="fromJspTag"><%=jspTag%></div>
<div>Open the browser console to see the tags.</div>
</body>
</html>

0 comments on commit e577b2c

Please sign in to comment.