Java thymeleaf 关于 th:replace、th:include、th:insert 三者的区别

kelvin · 2022-07-12 15:16:29 · 热度: 16

thymeleaf 关于 th:replace、th:include、th:insert 三者的区别

  • th:insert :保留自己的主标签,保留 th:fragment 的主标签。
  • th:replace :不要自己的主标签,保留 th:fragment 的主标签。
  • th:include :保留自己的主标签,不要 th:fragment 的主标签。(官方3.0后不推荐)

例子源码:

<footer th:fragment="copy_www_codercto_com">  
    <div>码农网</div>
</footer>

使用:

<div th:insert="footer :: copy_www_codercto_com"></div>  

<div th:replace="footer :: copy_www_codercto_com"></div>  

<div th:include="footer :: copy_www_codercto_com"></div>

结果:

<div>  
    <footer>  
              <div>码农网</div>
    </footer>    
</div>    

<footer>  
   <div>码农网</div>
</footer>    

<div>  
    <div>码农网</div> 
</div>
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册