springboot整合themeleaf
时间: 2023-06-05 18:48:05 浏览: 69
Spring Boot可以很方便地整合Thymeleaf模板引擎,只需要在pom.xml文件中添加Thymeleaf依赖,然后在application.properties文件中配置Thymeleaf相关属性即可。
具体步骤如下:
1. 在pom.xml文件中添加Thymeleaf依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
2. 在application.properties文件中配置Thymeleaf相关属性:
```
# Thymeleaf配置
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.template-resolver-order=1
spring.thymeleaf.view-names=*.html
```
3. 在Controller中使用Thymeleaf模板引擎:
```
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "Hello, Thymeleaf!");
return "home";
}
}
```
4. 在templates目录下创建home.html模板文件:
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Home</title>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>
```
以上就是Spring Boot整合Thymeleaf的基本步骤。