在所有项目中页面的互相包含是一项非常重要的技术支持,而在thymeleaf也同样支持有数据的包含处理,而对于包含操作在thymeleaf模板之中提供有两种支付语法
th:replace; 是使用标签进行替换 原始的宿主标签还在 但是包含标签不在;
th:include; 是进行包含 原始的宿主标签消失 而保留包含的标签
1.0 既然要定义被包含的页面 于是建立“src/main/resources/templates/commons/footer”页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Title</title>
</head>
<footer th:fragment="companyInfo">
<p style="color: red">zw(程序生活)</p>
</footer>
</html>
2.0 随后要进行页面的包含处理
<div th:replace="@{/commons/footer}::companyInfo"></div>
<br/>
<div th:include="@{/commons/footer}::companyInfo"></div>
3.0 在很多的开发之中都需要向被包含页面进行参数的传递 在thymeleaf 之中也可以实现一样的处理操作形式 使用 th:with的处理模式完成
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Title</title>
</head>
<footer th:fragment="companyInfo">
<p style="color: red">zw(程序生活)</p>
<!--/*@thymesVar id="id" type="com"*/-->
<p th:text="${id}"/>、<p th:text="${suid}"></p>
<!--/*@thymesVar id="suid" type="com"*/-->
</footer>
</html>
4.0 而后在进行包含处理的时候可以按照如下的方式进行参数的传递
<div th:include="@{/commons/footer}::companyInfo" th:with="id=3,suid=35"></div>
githua 代码如下 https://github.com/zhouwei520/SpringbootDemo/tree/master/demo9