天天看点

thymeleaf中include和replace的使用与区别及传参

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>
           

传参:

<div th:fragment="Five(onevar,twovar)">
        <p th:text="${onevar} + ' - - - - - ' + ${twovar}"></p>
    </div>
           

引入模板如:

<div th:include="myfrag::Five ('ThisOne','ThisTwo')"></div>
           

效果:

thymeleaf中include和replace的使用与区别及传参

继续阅读