参考:https://blog.csdn.net/qq_43634655/article/details/126424130
从session取值
- 后端代码
Map<String,Object> map = new HashMap<>();
map.put("id","1001");
map.put("name","小虎");
map.put("age","18");
map.put("message","完美世界");
map.put("code","200");
session.setAttribute("map",map);
- 前端代码
<!--前面要加session-->
<h2 th:text="${session.map.name}"></h2>
从model取值
- 后端代码
List<User> allUser = userMapper.findAllUser();
model.addAttribute("userList",allUser);
- 前端代码
<tbody th:each="user:${userList}">
<tr>
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.password}"></td>
<td th:text="${user.sex == 1 ? '男':'女' }"></td>
<td th:text="${user.sex}"></td>
<td th:text="${#dates.format(user.birthday,'yyyy-MM-dd')}"></td>
<td th:text="${#dates.format(user.createTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
<td th:text="${#dates.format(user.updateTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
<td>
<a class="btn btn-sm btn-primary">更新</a>
<a class="btn btn-sm btn-warning">删除</a>
</td>
</tr>
</tbody>
从request取值
- 后端代码
request.setAttribute("requestAge", 100);
- 前端代码
<span style="color: red;" th:text="${requestAge}"></span><br />
从servletContext取值
- 后端代码
servletContext.setAttribute("applicationNum", 1);
- 前端代码
<span style="color: red;" th:text="${application.applicationNum}"></span><br />
标签:map,作用域,代码,setAttribute,获取,thymeleaf,put,取值,前端
From: https://www.cnblogs.com/hasome/p/17698666.html