内容简介:举个栗子用上面三种方式引用下面这段:引用:
Thymeleaf 模板布局和属性优先级
th:insert,th:replace,th:include三者的区别
- th:insert 它将简单地插入指定的片段作为正文的标签
- th:replace 用指定实际片段来替换其主标签
- th:include 类似于 th:insert ,但不是插入片段它只插入此片段的内容(3.x版本后,不再推荐使用)
举个栗子用上面三种方式引用下面这段:
<footer th:fragment="copy"> © 2019 https://blog.eunji.cn "爱敲代码的猫" </footer>
引用:
<body> ... <div th:insert="footer :: copy"></div> <div th:replace="footer :: copy"></div> <div th:include="footer :: copy"></div> </body>
最终会是下面这种效果:
<body>
...
<div>
<footer>
© 2019 https://blog.eunji.cn "爱敲代码的猫"
</footer>
</div>
<footer>
© 2019 https://blog.eunji.cn "爱敲代码的猫"
</footer>
<div>
© 2019 https://blog.eunji.cn "爱敲代码的猫"
</div>
</body>
属性优先级
th:* 在同一个标签中写入多个属性会发生什么?例如:
<ul>
<li th:each="item : ${items}" th:text="${item.description}">Item description here..</li>
</ul>
我们希望该 th:each 属性在之前执行, th:text 以便我们得到我们想要的结果,但是考虑到 HTML / XML 标准没有给标记中的属性写入的顺序赋予任何意义, 优先级 必须在属性本身中建立机制,以确保它将按预期工作。
因此,所有 Thymeleaf 属性都定义了一个数字优先级,它确定了它们在标记中执行的顺序。这个顺序是:
| 顺序 | 功能 | 属性 |
|---|---|---|
| 1 | 片段包含 | th:insert th:replace |
| 2 | 片段的迭代 | th:each |
| 3 | 条件判断 | th:if th:unless **th:switch ** th:case |
| 4 | 局部变量的定义 | th:object th:with |
| 5 | 通用属性修改 | th:attr th:attrprepend th:attrappend |
| 6 | 特定属性修改 | th:value th:href th:src ... |
| 7 | 文本(标签主体修改) | th:text th:utext |
| 8 | 片段规范 | th:fragment |
| 9 | 片段删除 | th:remove |
这个优先级机制意味着如果属性位置被反转,上面的迭代片段将给出完全相同的结果(虽然它的可读性稍差):
<ul>
<li th:text="${item.description}" th:each="item : ${items}">Item description here..</li>
</ul>
本文由Aquan 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: 三月 8,2019
以上所述就是小编给大家介绍的《Thymeleaf 模板布局和属性优先级》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 如何确定需求的优先级?
- RabbitMQ之优先级消息队列
- [译]HTTP/2的优先级
- Spring Boot RabbitMQ - 优先级队列
- CSS 基础(盒模型、选择器、权重、优先级)
- Python中栈、队列和优先级队列的实现
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Cascading Style Sheets 2.0 Programmer's Reference
Eric A. Meyer / McGraw-Hill Osborne Media / 2001-03-20 / USD 19.99
The most authoritative quick reference available for CSS programmers. This handy resource gives you programming essentials at your fingertips, including all the new tags and features in CSS 2.0. You'l......一起来看看 《Cascading Style Sheets 2.0 Programmer's Reference》 这本书的介绍吧!