1.引入依赖
<!-- Thymeleaf 基于3.x版本开发--> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency>
2.创建Controller
controller 使用注释@Controller 不可以用@RestController
@Controller
@RequestMapping("/test")
public class HelloController {
@RequestMapping("/html/test")
public String getHtml(Model model){
model.addAttribute("msg","测试文件:你好");
// 对应 html文件名
return "theShy";
}
}
3.创建html文件
html文件必须放入templates文件夹下访问,
编写测试model
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Baobao</title>
</head>
<body>
<!--html所有元素都能被替换-->
<div th:text="${msg}"></div>
</body>
</html>