1、添加起步依赖
<!-- thymeleaf模块集成 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
这里默认的版本是2.1.1,如果需要改成最新版本
在github中最新版本
添加下面配置即可
<properties>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
</properties>
2、thymeleaf自动配置
源码路径org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration
在源码中可以看见默认路径前缀classpath:/templates、后缀.html
3、界面跳转
接口
@RequestMapping("/hello")
public String helloPage(){
return "hello";
}
界面,一定要建在templates路径下
成功跳转
3、thymeleaf使用
3.1、导入thymeleaf命名空间
<html lang="en" xmlns: th = "http://www.thymeleaf.org">
3.2、将我们前面的hello接口调整如下
@RequestMapping("/hello")
public String helloPage(Map<String, Object> map){
map.put("hello", "你好");
return "hello";
}
3.3、在界面获取hello属性
<div th:text="${hello}"></div>
看见你好已经返回到界面上了!
更多thymeleaf语法可以下载官方提供的pdf文档。也可以在我的网盘下载 :usethymeleaf 提取码 zc7h