一、controller
@Controller
public class ThymeleafController
{
@RequestMapping("/test1")
public String test1(Model model)
{
model.addAttribute("msg","hello thymeleaf");
return "index";
}
}
二、pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
三、application.properties
#关闭模板缓存,方便开发调试,上线时可以关闭
spring.thymeleaf.cache=false
#使用遗留的html5以去掉html标签的校验
spring.thymeleaf.mode=LEGACYHTML5
四、index.html
注意要放在resources目录下,约定优先,加上关键的命名空间xmlns:th=“http://www.thymeleaf.org”
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>thymeleaf</title>
</head>
<body>
<div th:text="${msg}">
hehe
</div>
</body>
</html>