Spring Boot集成Thymeleaf模板引擎实战指南
2025-07-05 03:47:50作者:宣利权Counsellor
前言
在现代Web应用开发中,模板引擎是不可或缺的组件。Thymeleaf作为一款优秀的Java模板引擎,与Spring Boot框架完美集成,能够帮助开发者快速构建动态Web页面。本文将详细介绍如何在Spring Boot项目中集成和使用Thymeleaf模板引擎。
项目概述
本项目演示了Spring Boot如何集成Thymeleaf模板引擎,实现了一个简单的用户登录功能。通过这个示例,开发者可以学习到:
- Thymeleaf的基本配置
- 控制器与模板的交互方式
- 常用Thymeleaf语法
- 页面布局和片段复用
环境准备
依赖配置
在项目的pom.xml文件中,我们需要添加以下关键依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-thymeleaf
是Spring Boot对Thymeleaf的自动配置支持,而spring-boot-starter-web
则提供了Web开发所需的基础功能。
核心配置
application.yml配置
在application.yml中,我们可以对Thymeleaf进行详细配置:
spring:
thymeleaf:
mode: HTML
encoding: UTF-8
servlet:
content-type: text/html
cache: false
关键配置项说明:
mode
: 设置模板模式为HTMLencoding
: 指定模板编码为UTF-8cache
: 开发阶段建议关闭缓存,方便模板修改后立即生效
控制器实现
用户控制器
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {
@PostMapping("/login")
public ModelAndView login(User user, HttpServletRequest request) {
ModelAndView mv = new ModelAndView();
mv.addObject(user);
mv.setViewName("redirect:/");
request.getSession().setAttribute("user", user);
return mv;
}
@GetMapping("/login")
public ModelAndView login() {
return new ModelAndView("page/login");
}
}
这个控制器处理用户登录相关请求:
- GET
/user/login
: 返回登录页面 - POST
/user/login
: 处理登录表单提交,将用户信息存入session并重定向到首页
首页控制器
@Controller
@Slf4j
public class IndexController {
@GetMapping(value = {"", "/"})
public ModelAndView index(HttpServletRequest request) {
ModelAndView mv = new ModelAndView();
User user = (User) request.getSession().getAttribute("user");
if (ObjectUtil.isNull(user)) {
mv.setViewName("redirect:/user/login");
} else {
mv.setViewName("page/index");
mv.addObject(user);
}
return mv;
}
}
首页控制器实现了简单的访问控制:
- 如果用户未登录(session中没有user对象),重定向到登录页面
- 如果用户已登录,显示欢迎页面
模板实现
登录页面(login.html)
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<header th:replace="~{common/head :: header}"></header>
<body>
<div id="app" style="margin: 20px 20%">
<form action="/demo/user/login" method="post">
用户名<input type="text" name="name" placeholder="用户名"/>
密码<input type="password" name="password" placeholder="密码"/>
<input type="submit" value="登录">
</form>
</div>
</body>
</html>
首页(index.html)
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<header th:replace="~{common/head :: header}"></header>
<body>
<div id="app" style="margin: 20px 20%">
欢迎登录,<span th:text="${user.name}"></span>!
</div>
</body>
</html>
模板技术要点
- 命名空间声明:
xmlns:th="http://www.thymeleaf.org"
声明Thymeleaf命名空间 - 片段复用:
th:replace="~{common/head :: header}"
复用公共头部 - 变量渲染:
th:text="${user.name}"
显示用户名称
Thymeleaf最佳实践
- 模板组织:建议将模板按照功能模块组织在templates目录下
- 公共片段:将公共部分(如头部、尾部)提取为单独片段,便于复用
- 开发阶段:关闭缓存(
spring.thymeleaf.cache=false
) - 表单处理:使用Thymeleaf的表单绑定功能简化开发
总结
通过本项目的实践,我们学习了如何在Spring Boot中集成Thymeleaf模板引擎。Thymeleaf作为一款功能强大且易于学习的模板引擎,与Spring Boot的完美结合使得Web开发变得更加高效。开发者可以根据项目需求,进一步探索Thymeleaf的高级特性,如布局方言、条件判断、循环迭代等功能,构建更加复杂的Web应用。
对于想要深入学习Thymeleaf的开发者,建议参考官方文档,了解更多的表达式语法和高级用法。