JSP:JavaScript模板文字不起作用



我尝试使用模板文字

预期结果为"测试数据测试"

但是,它不起作用

如何在 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>

关键点是'\'

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

您的代码有两个问题:

首先,你需要使用"'而不是 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>

最新更新