1.公共片段
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>公共片段</title>
</head>
<body>
<!--声明代码片段-->
<div th:fragment="header_comment">
<p>
这是声明公共片段
</p>
</div>
<!--通过选择器声明-->
<div id="header_comment_id">
<p>
这是id声明公共片段
</p>
</div>
</body>
</html>
2.引入公共片段
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>模板文件</title>
</head>
<body>
<h2>成功</h2>
<hr/>
<!--th:text 设置p标签的标签体的内容-->
<p th:text="${name}">显示名字</p>
<!--引入公共代码片段
通过th:fragment声明的
注意:header 是文件名
header_comment 是声明的属性值
-->
<div th:replace="header :: header_comment"></div>
<!--通过id选择器进行声明代码片段-->
<!--注意:header 是文件名-->
<!--header_comment_id是声明的那个id-->
<div th:replace="header :: #header_comment_id" class="aaa"></div>
<!--th:insert 和 th:replace的区别-->
<!--th:insert和th:replace都可以引入片段,两者的区别在于-->
<!--th:insert-》保留引入时使用的标签 (整个插入过来)-->
<!--th:replace-》不保留引入时使用的标签,将声明片段直接覆盖当前引用标签(整个拿过来进行替换)-->
<h2 th:insert="header :: #header_comment_id" class="bbb"></h2>
</body>
</html>
关键-》th:fragmen声明片段 th:insert 和th:replace引入片段
th:insert和th:replace区别
有两种方式声明引入