如同 JSP 中 JSTL 的 <c:set var=“j” value=“${1}”/> 标签可以用于设置变量值和对象属性一样,Thymeleaf 中可以使用 th:with 进行指定局部变量,局部变量是指定义在模版⽚段中的变量,并且该变量的作⽤域为所在的模版⽚段。
<!--后台传值:model.addAttribute("userList", userList),userList 中有5个User--> <div th:with="userFirst=${userList[0]}"> <p>我:<span th:text="${userFirst}"></span></p> </div>
<div th:with="curPage=${#httpServletRequest.getParameter('page')}"> <h3>当前页码:<span th:text="${curPage}"></span></h3> </div>