jsp:javascript模板文字不起作用

时间:2018-10-05 07:52:27

标签: javascript java jsp ecmascript-6 template-literals

我尝试使用模板文字

预期结果为“测试数据测试”

但是,它不起作用

如何在jsp上使用它?

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script>
var t = "TEST";

console.log(`테스트 데이터  ${t} `);
</script>
<body>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

您的代码有两个问题:

首先,您需要使用"'而不是 Then you need to can not set parameter in javascript side and get it via $ {t} ,due to $ {t}`是EL表达式

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<% String t="TEST"; %> <!-- set value in the server side -->
<script>
console.log("테스트 데이터  ${t}");
</script>
<body>

</body>
</html>

答案 1 :(得分:0)

关键点是'\'

console.log(`테스트 데이터  \${t} `);