spring boot和thymeleaf如何连接
时间: 2023-11-27 08:54:21 浏览: 97
连接Spring Boot和Thymeleaf需要进行以下步骤:
1. 添加Thymeleaf依赖项
在Maven项目的pom.xml文件中添加以下依赖项:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
2. 配置Thymeleaf模板引擎
在application.properties文件中添加以下配置:
```properties
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
```
3. 创建Thymeleaf模板
在src/main/resources/templates目录下创建一个HTML文件,例如index.html。
4. 创建Controller类
创建一个@Controller注释的类,并在该类中创建一个@RequestMapping注释的方法,该方法返回要呈现的页面的名称,例如index。
```java
@Controller
public class HomeController {
@RequestMapping("/")
public String index() {
return "index";
}
}
```
5. 运行应用程序
现在可以运行Spring Boot应用程序并访问http://localhost:8080/,它将呈现index.html模板。
阅读全文
相关推荐


















