属性优先级
当在同一个标签中写入多个th:*属性时,会发生什么? 对于下面的例子:
<ul>
<li th:each="item : ${items}" th:text="${item.description}">Item description here...</li>
</ul>
我们期望th:each属性在th:text之前执行,从而可以得到了我们想要的结果,但是鉴于 HTML/XML标准并未对标签中的属性的顺序给出任何的定义,所以必须在属性中建立优先级(precedence)机制
以确保这将按预期工作。
所以,所有的Thymeleaf属性定义一个数字优先级,它建立了它们在标签中执行的顺序。 这个是列表:
| Order | Feature | Attributes |
|---|---|---|
| 1 | Fragment inclusion | th:insertth:replace |
| 2 | Fragment iteration | th:each |
| 3 | Conditional evaluation | th:ifth:unlessth:switchth:case |
| 4 | Local variable definition | th:objectth:with |
| 5 | General attribute modification | th:attrth:attrprependth:attrappend |
| 6 | Specific attribute modification | th:valueth:hrefth:src... |
| 7 | Text (tag body modification) | th:textth:utext |
| 8 | Fragment specification | th:fragment |
| 9 | Fragment removal | th:remove |
这个优先机制意味着如果属性位置被反转,上述迭代片段将给出完全相同的结果(尽管它的可读性稍差):
<ul>
<li th:text="${item.description}" th:each="item : ${items}">Item description here...</li>
</ul>