Spring Boot 视图层与模板引擎
概述
在 Spring Boot Web 应用中,视图层主要负责页面渲染、数据展示以及用户交互。Spring Boot 本身不强制绑定特定的模板引擎,而是提供了对多种流行引擎的自动配置支持,其中 Thymeleaf、Freemarker 和 Velocity 是最常用的选择。
集成 Thymeleaf
Thymeleaf 是 Spring Boot 默认推荐的模板引擎,特别适合现代 HTML5 应用,它允许在浏览器中直接预览静态页面。
依赖与配置
首先需要在 pom.xml 中添加 Starter 依赖,并在配置文件中指定模板路径。
pom.xml
<dependencies>
<!-- Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Thymeleaf 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Data JPA 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<>spring-boot-starter-data-jpa
com.h2database
h2
runtime
org.springframework.boot
spring-boot-starter-test
test


