forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-6670: Reading a JSP variable from JavaScript (eugenp#14337)
- Loading branch information
1 parent
4746fdc
commit e577b2c
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...spring-boot-jsp/src/main/java/com/baeldung/boot/jsp/controller/JSPVariableController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-el.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
16 changes: 16 additions & 0 deletions
16
spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-jsp.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
19 changes: 19 additions & 0 deletions
19
spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/by-jstl.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
19 changes: 19 additions & 0 deletions
19
spring-boot-modules/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/jsp-var/to-dom.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |