日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

th:replace th:include

發布時間:2024/3/24 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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">&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的寫法都支持表達式寫法:
<div th:include="footer :: (${user.isAdmin}? #{footer.admin} : #{footer.normaluser})"></div>
  • 1

不使用th:fragment來引用模塊

... <div id="copy-section"> &copy; 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。?
舉個栗子:

<footer th:fragment="copy"> &copy; 2011 The Good Thymes Virtual Grocery </footer>
  • 1
  • 2
  • 3

引入界面:

<body> ... <div th:include="footer :: copy"></div> <div th:replace="footer :: copy"></div> </body>
  • 1
  • 2
  • 3
  • 4
  • 5

結果是:

<body> ... <div> &copy; 2011 The Good Thymes Virtual Grocery </div> <footer> &copy; 2011 The Good Thymes Virtual Grocery </footer> </body>

總結

以上是生活随笔為你收集整理的th:replace th:include的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。