首先先在pom中引入thymeleaf的依赖
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
yml的配置为
spring:
#模板引擎
thymeleaf:
mode: HTML5
encoding: UTF-8
cache: false
默认使用html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<footer th:fragment="copy" id="1">
this is footer
</footer>
<!--在本标签内引入-->
<div th:insert="::copy"></div>
<!--全部引入,替换掉本标签内容-->
<div th:replace="::copy"></div>
<!--引入时用本标签替换掉要引用的模块标签-->
<div th:include="::copy"></div>
结果为
<!--在本标签内引入-->
<div>
<footer id="1">
this is footer
</footer>
</div>
<!--全部引入,替换掉本标签内容-->
<footer id="1">
this is footer
</footer>
<!--引入时用本标签替换掉要引用的模块标签-->
<div>
this is footer
</div>
<!--怎么使用?-->
<!--templatename::selector:”::”前面是模板文件名,后面是选择器;选择器是自己定义的名称,例如<footer th:fragment="copy" id="1"></foot>-->
<!--::selector:只写选择器,这里指fragment名称,则加载本页面对应的fragment-->
<!--templatename:只写模板文件名,则加载整个页面-->
<!--引入common文件夹下pagination.html文件中的test1选择器-->
<div th:replace="common/pagination::test1"></div>
<!--引入本文件中的名为copy选择器-->
<div th:replace="::copy"></div>
<!--引入整个文件,文件在common文件夹下的common.html-->
<div th:include="common/common"></div>
总结:
可以根据自己的需要使用insert/include/replace,来引入本文件,其它文件的选择器,或页面
来进行代码的复用
</body>
</html>