Thymelead th:with 局部变量 与 属性优先级

本文详细介绍了Thymeleaf模板引擎中局部变量的定义和使用方法,包括如何使用th:with属性创建局部变量,以及局部变量的作用域和属性优先级等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

局部变量

如同 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(属性)
1Fragment inclusionth:insert 、th:replace
2Fragment iterationth:each
3Conditional evaluationth:if 、th:unless、th:switch、 th:case
4Local variable definitionth:object 、th:with
5General attribute modificationth:attr、 th:attrprepend、th:attrappend
6Specific attribute modificationth:value、 th:href、 th:src…
7Text (tag body modification)th:text 、th:utext
8Fragment specificationth:fragment
9Fragment removalth: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>
<think>我们正在讨论Thymeleaf模板引擎中的`th:with`属性,它用于声明局部变量。用户的问题是:是否可以在`th:with`中已经定义的局部变量基础上,再次定义新的局部变量?换句话说,就是在一个`th:with`属性中,能否使用前面定义的变量来定义后面的变量?根据Thymeleaf的文档和示例,答案是肯定的。在一个`th:with`属性中,可以声明多个局部变量,并且这些变量的声明是顺序执行的,因此后面声明的变量可以使用前面已经声明的变量。例如,我们可以这样写:```html<divth:with="firstPer=${persons[0]},fullName=${firstPer.firstName+''+firstPer.lastName}"><p>Firstperson:<spanth:text="${fullName}"></span></p></div>```在这个例子中,我们首先声明了`firstPer`变量,然后在同一个`th:with`属性中,我们声明了第二个变量`fullName`,它使用了`firstPer`变量来构建全名。另外,我们也可以在一个元素上使用多个`th:with`属性吗?注意,同一个元素上不能有多个`th:with`属性,因为Thymeleaf不允许同一个元素上有多个相同属性的处理。但是,我们可以通过嵌套元素来实现类似的效果。例如:```html<divth:with="firstPer=${persons[0]}"><divth:with="fullName=${firstPer.firstName+''+firstPer.lastName}"><p>Firstperson:<spanth:text="${fullName}"></span></p></div></div>```但是,在同一个`th:with`属性中,用逗号分隔多个变量声明,并且后面的变量可以使用前面声明的变量,这种方式更简洁。注意:在同一个`th:with`中,变量的作用域是整个当前元素及其子元素,并且变量声明的顺序决定了它们之间的依赖关系。下面是一个更复杂的例子,展示了在一个`th:with`中定义多个变量,并且后面的变量使用前面的变量:```html<divth:with="total=${#lists.size(items)},firstItem=${total>0?items[0]:null},firstItemName=${firstItem!=null?firstItem.name:'Noitems'}"><p>Totalitems:<spanth:text="${total}"></span></p><p>Firstitemname:<spanth:text="${firstItemName}"></span></p></div>```在这个例子中,我们定义了三个变量:`total`(列表大小),`firstItem`(如果列表非空则取第一个元素,否则为null),`firstItemName`(根据第一个元素是否存在来显示名称或默认文本)。注意,后面的变量都依赖于前面声明的变量。因此,回答用户的问题:在Thymeleaf的`th:with`中,可以基于已定义的局部变量再次定义新的变量,只要在同一个`th:with`属性中按顺序声明即可。另外,需要注意的是,在Thymeleaf3.0中,局部变量的作用域是声明它的标签及其子标签。在同一个元素中,多个变量声明之间用逗号分隔,并且后面的变量可以引用前面声明的变量。引用说明:根据用户提供的引用[2]和[3],我们了解到`th:with`用于声明局部变量,并且作用域在定义它的模版片段内。我们的回答这些引用一致,因此我们可以添加引用标记[^2][^3]。最后,生成相关问题:1.Thymeleaf中局部变量`th:with`的作用域规则是什么?2.如何在Thymeleaf中修改局部变量的值?3.Thymeleaf中除了`th:with`,还有其他方式定义局部变量吗?注意:用户提供的引用[4]中展示了在一个`th:with`中定义多个变量(用逗号分隔),但没有展示变量之间的依赖关系。不过,根据Thymeleaf的官方文档,这是支持的。因此,我们可以这样总结回答:</think>在 Thymeleaf 的 `th:with` 中,**可以基于已定义的局部变量再次定义新变量**,这是完全支持的语法特性。以下是具体实现方式和注意事项: ### ✅ 实现方法 在单个 `th:with` 属性中用逗号分隔多个变量声明,后续变量可直接使用前面定义的变量: ```html <div th:with=" firstUser=${userList[0]}, userName=${firstUser.name}, <!-- 使用前一个变量 firstUser --> userAge=${firstUser.age + 1} <!-- 使用前一个变量并进行计算 --> "> 姓名:<span th:text="${userName}"></span> <!-- 输出第一个用户的姓名 --> 年龄+1:<span th:text="${userAge}"></span> <!-- 输出年龄加1的结果 --> </div> ``` ### ⚠️ 关键规则 1. **声明顺序决定依赖关系** - 后面声明的变量可使用前面定义的变量 - 前面声明的变量**不能**使用后面定义的变量 ```html <!-- ✅ 正确:后变量使用前变量 --> <div th:with="a=10, b=a+5">...</div> <!-- ❌ 错误:前变量使用后变量 --> <div th:with="b=a+5, a=10">...</div> ``` 2. **作用域限定在当前标签内** ```html <div th:with="x=5, y=x*2"> <!-- x,y 在此div内有效 --> <span th:text="${y}"></span> <!-- 输出10 --> </div> <span th:text="${x}"></span> <!-- 报错:x 超出作用域 --> ``` 3. **支持复杂表达式** ```html <div th:with=" total=${#lists.size(items)}, avg=${total > 0 ? sum/total : 0} <!-- 使用total变量 --> "> 平均值:[[${avg}]] </div> ``` ### 🔄 变量覆盖规则 在嵌套作用域中,内层可覆盖外层变量(但不影响外层值): ```html <div th:with="value=100"> 外层:[[${value}]] <!-- 输出100 --> <div th:with="value=200, newValue=value*2"> 内层:[[${value}]] <!-- 输出200 (覆盖外层) --> 计算:[[${newValue}]] <!-- 输出400 --> </div> 外层恢复:[[${value}]] <!-- 输出100 --> </div> ``` ### 💡 实用技巧 **链式生成多级变量**: ```html <div th:with=" page=${param.page}, start=${page * 10 - 10}, <!-- 使用page变量 --> end=${start + 9} <!-- 使用start变量 --> "> 记录区间:[[${start}]] - [[${end}]] </div> ``` > 此特性在 Thymeleaf 3.x 中稳定支持,官方文档明确允许在单个 `th:with` 中声明多个关联变量[^2][^3][^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值