局部变量
如同 JSP 中 JSTL 的 <c:set var=“j” value="${1}"/> 标签可以用于设置变量值和对象属性一样,Thymeleaf 中可以使用 th:with 进行指定局部变量,局部变量是指定义在模版⽚段中的变量,并且该变量的作⽤域为所在的模版⽚段。
< tr th:each=“user : ${userList}”> … </ tr>
1)如上所示 th:each 迭代中的 user 其实就是局部变量,仅在 标签范围内可⽤,包括所有子元素,可⽤于在该标签中执⾏优先级低于 th:each 的任何其他 th:* 属性。
Thymeleaf 提供 th:with 属性声明局部变量,其语法与属性值分配类似:
<div th:with="firstPer=${persons[0]}">
<p>The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.</p>
</div>
当 th:with 被处理时,firstPer 变量被创建为局部变量并被添加到来⾃上下⽂ map 中,以便它可以与上下⽂中声明的任何其他变量⼀起使⽤,但只能在包含的 < div> 标签内使⽤。
2)可以使用同时定义多个变量,用逗号隔开:
<div th:with="firstPer=${persons[0]},secondPer=${persons[1]}">
<p>The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.</p>
<p>But the name of the second person is<span th:text="${secondPer.name}">Marcus Antonius</span>.</p>
</div>
3) th:with 属性允许重⽤在同⼀属性中定义的变量:
<div th:with="company=${user.company + ' Co.'},account=${accounts[company]}">...</div>
编码示例
<!--定义变量 userName='Admin',可在整个 body 中进行使用-->
<body th:with="userName='Admin'">
<p>当前登录用户:<span th:text="${userName}"></span></p>
<!--后台传值:model.addAttribute("userList", userList),userList 中有5个User-->
<div th:with="userFirst=${userList[0]}">
<p>我:<span th:text="${userFirst}"></span></p>
</div>
<!--一次定义三个变量:withId、withInfo、isMarry-->
<!--后台传值:model.addAttribute("id", null),于是 withId 的值也为nuLl-->
<!--后台传值:model.addAttribute("info", "Love you 中国");-->
<!--后台传值:model.addAttribute("isMarry", true);-->
<div th:with="withId=${id},withInfo=${info},isMarry=${isMarry}">
<p>withId:<span th:text="${withId}"></span></p>
<p>withInfo:<span th:text="${withInfo}"></span></p>
<p>isMarry:<span th:text="${isMarry}"></span></p>
<p></p>
</div>
<!--一次定义两个变量 id 与 flag ,且 flag 的值使用到了 id 变量-->
<div th:with="id=9527,flag=${id}+'_flag'">
<p>id:<span th:text="${id}"></span></p>
<p>flag:<span th:text="${flag}"></span></p>
</div>
</body>

属性优先级
当在同⼀个标签中写⼊多个 th:* 属性时会发⽣什么? 例如:
<ul>
<li th:each="item : ${items}" th:text="${item.description}">Item description here...</li>
</ul>
期望是 th:each 属性在 th:text 之前执⾏,以便得到想要的结果,但是 HTML / XML 标准对于标签属性的渲染顺序没有任何定义, 因此 Thyme Leaf 如果要达到这种效果,必须在属性本身中建⽴优先机制,以确保这些属性按预期⼯作。因此,所有的Thymeleaf 属性都定义⼀个数字优先级,以便确定在标签中按顺序执⾏。优先级清单如下:
| Order(顺序) | Feature(特征) | Attributes(属性) |
|---|---|---|
| 1 | Fragment inclusion | th:insert 、th:replace |
| 2 | Fragment iteration | th:each |
| 3 | Conditional evaluation | th:if 、th:unless、th:switch、 th:case |
| 4 | Local variable definition | th:object 、th:with |
| 5 | General attribute modification | th:attr、 th:attrprepend、th:attrappend |
| 6 | Specific attribute modification | th:value、 th:href、 th:src… |
| 7 | Text (tag body modification) | th:text 、th:utext |
| 8 | Fragment specification | th:fragment |
| 9 | Fragment removal | th:remove |
这个优先机制意味着与属性位置并无关系,只与属性名有关:
<ul>
<li th:each="item : ${items}" th:text="${item.description}">Item description here...</li>
</ul>
等价于
<ul>
<li th:text="${item.description}" th:each="item : ${items}">Item description here...</li>
</ul>
本文详细介绍了Thymeleaf模板引擎中局部变量的定义和使用方法,包括如何使用th:with属性创建局部变量,以及局部变量的作用域和属性优先级等内容。
345

被折叠的 条评论
为什么被折叠?



