thymeleaf中th:insert、th:replace、th:include的區別


關於thymeleaf中th:insert、th:replace、th:include的區別

1. th:insert:保留自己的主標簽,保留th:fragment的主標簽

 1 需要替換的片段內容:
 2 <footer th:fragment="copy">
 3    <script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script>
 4 </footer>
 5 
 6 導入片段:
 7   <div th:insert="footer :: copy"></div>
 8 
 9 
10 結果為:
11 <div>
12     <footer>
13        <script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script>
14     </footer>  
15 </div> 

2. th:replace:不保留自己的主標簽,保留th:fragment的主標簽

 1 需要替換的片段內容:
 2 <footer th:fragment="copy">
 3    <script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script>
 4 </footer>
 5 
 6 導入片段:
 7   <div th:replace="footer :: copy"></div>
 8 
 9 結果為:
10 <footer>
11   <script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script>
12 </footer>  

3. th:include:保留自己的主標簽,不保留th:fragment的主標簽(官方3.0不推薦)

 1 需要替換的片段內容:
 2 <footer th:fragment="copy">
 3    <script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script>
 4 </footer>
 5  
 6 導入片段:
 7   <div th:include="footer :: copy"></div>
 8   
 9 結果為:
10 <div>
11   <script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script>
12 </div>  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM