SpringBoot整合Thymeleaf
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 基础配置
#Thymeleaf
#是否启用缓存,开发环境不建议启用,生成环境建议启用
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
- 创建模板(thymeleaf.html)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Thymeleaf 模板</title>
</head>
<body>
<h2>Thymeleaf 模板</h2>
</body>
</html>
- 创建Controller
/**
* Author: avaos
*/
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
@Autowired
private ServerConstant settings;
@GetMapping
public String index(ModelMap modelMap){
modelMap.addAttribute("settings", settings);
return "thymeleaf";
}
- 测试
在浏览器中访问http://localhost/thymeleaf