Thymeleaf 教程

Thymeleaf 属性优先级

当你在同一个标签中写下一个以上的 th:* 属性时会发生什么?比如说:

<ul>
    <li th:each="item : ${items}" th:text="${item.description}">Item description here...</li>
</ul>

我们希望 th:each 属性在 th:text 之前执行,这样我们就能得到我们想要的结果,但鉴于 HTML/XML 标准没有对标签中属性的书写顺序赋予任何意义,因此必须在属性本身中建立一个优先机制,以确保这将按预期的方式工作。

因此,所有 Thymeleaf 属性都定义了一个数字优先级,它确定了它们在标签中执行的顺序。这个顺序是:

顺序特性属性
1Fragment inclusionth:replace
2Fragment iterationth:each
3Conditional evaluationth:case
4Local variable definitionth:with
5General attribute modificationth:attrappend
6Specific attribute modification...
7Text (tag body modification)th:utext
8Fragment specificationth:fragment
9Fragment removalth:remove

这种优先机制意味着,如果属性位置倒置,上述迭代片段将给出完全相同的结果(尽管它的可读性会稍差):

<ul>
    <li th:text="${item.description}" th:each="item : ${items}">Item description here...</li>
</ul>
说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号