springboot集成thymeleaf
时间: 2023-04-27 09:02:46 浏览: 164
Spring Boot可以很方便地集成Thymeleaf模板引擎,以下是简要的集成步骤:
1. 在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
2. 在application.properties文件中添加Thymeleaf相关配置:
```
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
```
3. 在Spring Boot应用程序中创建控制器,处理请求并返回模板名称:
```java
@Controller
public class MyController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "Hello World!");
return "home";
}
}
```
4. 创建Thymeleaf模板文件(如上面配置所示,模板文件位于classpath:/templates/目录下),使用Thymeleaf标记和语法来渲染模型数据:
```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的基本配置和使用。
阅读全文
相关推荐


















