一、首先我们需要导入thymeleaf的依赖,配置pom文件
<!--引入thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
二、thymeleaf不用重启配置
在启动配置里点击Edit Configurations
配置成如图所示
在Setting中把Build project automatically勾上,然后点击apply ok
最后我们需要在application.yml中配置缓存为false
spring:
thymeleaf:
# 模板的模式
mode: HTML5
encoding: UTF-8
servlet:
content-type: text/html
# 这个开发配置为false,避免改了模板还要重启服务器
cache: false
最后就可以进行测试啦
Controller
@Controller
public class HelloController {
@RequestMapping("hello")
public String hello(Model model) {
model.addAttribute("name","thymeleaf");
return "hello";
}
}
HTML
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>thymeleaf</title>
</head>
<body>
<p th:text="${name}">name</p>
<p th:text="'Hello!'+ ${name} + '!'">hello world</p>
<p th:text="|hello! ${name}!|">hello world</p>
</body>
</html>
当改变html中的值时不需要再重启了