th:replace th:include
生活随笔
收集整理的這篇文章主要介紹了
th:replace th:include
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文:https://blog.csdn.net/h1021456873/article/details/79085505
Thymeleaf html 導入(th:replace & th:include)
模板模塊導入
首先定義一個/WEBINF/templates/footer.html文件:
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><body><div th:fragment="copy">© 2011 The Good Thymes Virtual Grocery</div></body> </html>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
上面的代碼定義了一個片段稱為copy,我們可以很容易地使用th:include 或者 th:replace屬性包含在我們的主頁上:
<body> ... <div th:include="footer :: copy"></div> </body>- 1
- 2
- 3
- 4
include的表達式想當簡潔。這里有三種寫法:
- “templatename::domselector” 或者 “templatename::[domselector]”引入模板頁面中的某個模塊。
- “templatename”引入模板頁面。
- “::domselector” 或者 “this::domselector” 引入自身模板的模塊?
上面所有的templatename和domselector的寫法都支持表達式寫法:
- 1
不使用th:fragment來引用模塊
... <div id="copy-section"> © 2011 The Good Thymes Virtual Grocery </div> ...- 1
- 2
- 3
- 4
- 5
我們可以用css的選擇器寫法來引入
<body> ... <div th:include="footer :: #copy-section"></div> </body>- 1
- 2
- 3
- 4
th:include 和 th:replace的區別
th:include和th:replace都可以引入模塊,兩者的區別在于?
th:include:引入子模塊的children,依然保留父模塊的tag。?
th:replace:引入子模塊的所有,不保留父模塊的tag。?
舉個栗子:
- 1
- 2
- 3
引入界面:
<body> ... <div th:include="footer :: copy"></div> <div th:replace="footer :: copy"></div> </body>- 1
- 2
- 3
- 4
- 5
結果是:
<body> ... <div> © 2011 The Good Thymes Virtual Grocery </div> <footer> © 2011 The Good Thymes Virtual Grocery </footer> </body>總結
以上是生活随笔為你收集整理的th:replace th:include的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 必备压力变送器零位正迁移简单常识校验弄懂
- 下一篇: 2022年12月编程语言排行榜,最新数据