使用thymeleaf模板时,使用js来接收后台传过来的参数

1.先模拟一下跳转页面携带参数

 1 @Controller
 2 public class UserController {
 3     /**
 4      * 方法路径
 5      * 
 6      * @param model
 7      * @param map
 8      *            ModelMap对象主要用于传递控制方法处理数据到结果页面,也就是说我们把结果页面上需要的 数据放到ModelMap对象中即可,
 9      *            他的作用类似于request对象的setAttribute方法的作用,用来在一个请求过程中传递处理的数据。
10      * @return 返回路径
11      */
12     @RequestMapping("/user")
13     public Object getUser(Model model, ModelMap map) {
14         // model.addAttribute("user", "这是一个测试数据");
15         map.put("user", "这是一个测试数据");// 使用ModelMap传递数据
16         return "/user";
17     }
18 }

2.对应user.html页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>


<script type="text/javascript" th:inline="javascript">



    alert([[${user}]])
</script>
</body>
</html>

3.访问页面

在前台页面不使用th标签来接收参数 使用 [[${参数名}]] 的方式来接收    在<script>标签内 要加上  th:inline="javascript"

猜你喜欢

转载自www.cnblogs.com/hsszs/p/12106386.html