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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Thymeleaf th:include、th:replace使用

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Thymeleaf th:include、th:replace使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近做到頁面數據展示分頁的功能,由于每個模塊都需要分頁,所以每個頁面都需要將分頁的頁碼選擇內容重復的寫N遍,如下所示:


重復的代碼帶來的就是Ctrl+C,Ctrl+V ,于是了解了一下thymeleaf的fragment加載語法以及th:include、th:replace的區別,得以解決。

首先在pom.xml引入thymeleaf的依賴

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

將上述的重復信息抽取出來存為pagination.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 class="panelBar" th:fragment="pagination"><!--以下為公共部分--><div class="pages"><span>顯示</span><select class="combox" name="numPerPage" οnchange="navTabPageBreak({numPerPage:this.value})"><option value="1" th:selected="${pages.numPerPage}==1">1</option><option value="3" th:selected="${pages.numPerPage}==3">3</option><option value="5" th:selected="${pages.numPerPage}==5">5</option><option value="10" th:selected="${pages.numPerPage}==10">10</option><option value="100" th:selected="${pages.numPerPage}==100">100</option><option value="150" th:selected="${pages.numPerPage}==150">150</option><option value="200" th:selected="${pages.numPerPage}==200">200</option><option value="250" th:selected="${pages.numPerPage}==250">250</option></select><span id="fleeceRecordCounts" th:text="'共有'+${pages.totalCount}+''"></span></div><div id="fleece_page" class="pagination" th:attr="targetType=${pages.targetType},totalCount=${pages.totalCount},numPerPage=${pages.numPerPage},pageNumShown=${pages.pageNumShown},currentPage=${pages.currentPage}"></div></div></body> </html>

在其他頁面進行引用該公共模塊時如下:

<div class="panelBar" th:replace="pagination::pagination"></div>

注意:第一個pagination為上述公共部分的文件名,第二個pagination為th:fragment的值。這樣便可以解決公共部分代碼的抽取。

fragment加載語法如下:

templatename::selector:”::”前面是模板文件名,后面是選擇器
::selector:只寫選擇器,這里指fragment名稱,則加載本頁面對應的fragment
templatename:只寫模板文件名,則加載整個頁面

================== fragment語法 ============================= <!-- 語法說明 "::"前面是模板文件名,后面是選擇器 --><div th:include="template/footer::copy"></div><!-- 只寫選擇器,這里指fragment名稱,則加載本頁面對應的fragment --><div th:include="::#thispage"></div><!-- 只寫模板文件名,則加載整個頁面 --><div th:include="template/footer"></div> ================= 加載塊 ============================<span id="thispage">div in this page. </span>

th:include 和 th:replace都是加載代碼塊內容,但是還是有所不同

th:include:加載模板的內容: 讀取加載節點的內容(不含節點名稱),替換div內容
th:replace:替換當前標簽為模板中的標簽,加載的節點會整個替換掉加載他的div
公共部分如下:

<!-- th:fragment 定義用于加載的塊 --> <span th:fragment="pagination"> the public pagination </span>

引用時如下:

================= th:include 和 th:replace============================ <!-- 加載模板的內容: 讀取加載節點的內容(不含節點名稱),替換<div>的內容 --> <div th:include="pagination::pagination">1</div> <!-- 替換當前標簽為模板中的標簽: 加載的節點會整個替換掉加載他的<div> --> <div th:replace="pagination::pagination">2</div>

結果如下:

<!-- 加載模板的內容: 讀取加載節點的內容(不含節點名稱),替換<div>的內容 --> <div> the public pagination</div> <!-- 替換當前標簽為模板中的標簽: 加載的節點會整個替換掉加載他的<div> --> <span> the public pagination</span>

總結

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

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