1、在pom.xml中导入thymeleaf
<!--引入thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
需要将html页面放在templates下面,thymeleaf模板引擎就会自动渲染。
2、在html中使用thymeleaf需要导入名称空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3、导入webjars中的css样式
<link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
4、导入springboot中static目录下的自己的css样式
<link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">
5、在thymeleaf中发送请求
//发送http://localhost:8080/user/login请求
<form class="form-signin" action="dashboard.html" th:action="@{/user/login}" method="post">
...
</form>
6、thymeleaf公共页面的抽取
将工具的html代码片段写在common目录的 bar.html中。
第一种使用th:fragment
<div th:fragment="topbar"> topbar为起的片段名称
....
</div>
引用方法:
<div th:replace="common/bar::topbar"></div>
第二种:使用id选择器
<div id="sidebar">
....
</div>
引用方法:
<div th:replace="common/bar::#sidebar"></div>
th:insert:将公共片段整个插入到声明引入的元素中
th:replace:将声明引入的元素替换为公共片段
th:include:将被引入的片段的内容包含进这个标签中