一、Thymeleaf 模板引擎
SpringBoot 官方推荐服务端模板引擎,优势为自然模板,原生 HTML 可直接在浏览器预览,无需后端运行也能展示静态结构。
1. Thymeleaf 基础配置规则
- 依赖引入
XML
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 自动配置类:
ThymeleafAutoConfiguration - 默认页面存放路径:
classpath:/templates/,文件后缀固定为.html,可通过application.properties修改前缀、后缀、缓存等配置。 - HTML 页面必须引入命名空间才能使用 th 语法:
XML
<html lang="en" xmlns:th="http://www.thymeleaf.org">
2. 标准四大表达式
1 变量表达式 ${}
底层基于 OGNL/SpringEL,用于获取 Model、Session 域中存储的数据,作用于 HTML 属性。
- 语法:
${对象.属性} - 示例:
<span th:text="${book.author.name}">
2 选择(星号)表达式 *{}
依赖th:object预先绑定对象,简化重复对象的属性读取,等价于${绑定对象.属性}。
XML
<div th:object="${book}">
<span th:text="*{title}"></span>
</div>
3 国际化消息表达式 #{}
读取项目.properties国际化配置文件,通过 key 获取文本,支持传入动态参数。
- 语法:
#{配置key} - 示例:
#{main.title}
4 URL 表达式 @{}
自动拼接项目上下文路径,无需手动写项目名,支持 URL 传参,常用于 a 链接、form 表单、图片地址。
- 无参地址:
@{/order/list} - 带参地址:
@{/order/details(id=${orderId})} - 表单示例:
<form th:action="@{/createOrder}">
3. 表达式支持全量语法
1 字面量
- 文本:
'文本内容' - 数字:
10、3.14 - 布尔:
true、false - 空值:
null - 无引号标记:
user、main
2 文本操作
- 字符串拼接:
+ - 文本插值替换:
|姓名:${name}|
3 算术运算符
二元:+ - * / %;一元负号:-
4 布尔运算符
且:and;或:or;非:! / not
5 比较运算符
- 大小:
> < >= <=(等价别名:gt、lt、ge、le) - 等值:
== !=(等价别名:eq、ne)
6 条件运算符
- 简单 if:
(条件) ? 成立值 - 三目:
(条件) ? 成立 : 不成立 - 默认值:
(变量) ?: 默认值 - 综合示例:
java
'用户类型:' + (${user.isAdmin()} ? '管理员' : (${user.type} ?: '未知'))
4. Thymeleaf 标签大全
| 标签关键字 | 功能说明 | 使用案例 |
|---|---|---|
| th:id | 动态替换元素 id | <input th:id="'id_' + ${collect.id}"/> |
| th:text | 纯文本渲染,自动转义 HTML 标签 | <p th:text="${desc}"></p> |
| th:utext | 渲染带 HTML 格式的内容,不转义标签 | <div th:utext="${htmlStr}"></div> |
| th:object | 绑定对象,配合*{}取值 |
<div th:object="${user}"> |
| th:value | 表单 input 赋值 | <input th:value="${user.name}"> |
| th:onclick | 绑定点击事件 | <button th:onclick="getInfo()"> |
| th:each | 循环遍历集合,可获取循环状态 | <tr th:each="u,stat:${userList}"> |
| th:if | 条件成立则展示标签 | <a th:if="${userId == loginId}">编辑</a> |
| th:unless | 与 th:if 逻辑相反,条件不成立展示 | <a th:unless="${user}">去登录</a> |
| th:href | 动态链接地址,配合@{} |
<a th:href="@{/login}">登录</a> |
| th:switch/th:case | 多路分支判断 | <div th:switch="${gender}"><p th:case="1">男</p></div> |
| th:fragment | 定义公共代码片段 | <div th:fragment="footer"></div> |
| th:include | 引入片段,仅替换内部内容 | <div th:include="layout :: head"></div> |
| th:replace | 引入片段,替换当前整个标签 | <div th:replace="common :: header"></div> |
| th:selected | 下拉框默认选中 | <option th:selected="${id == item.id}"> |
| th:src | 图片、JS 资源路径 | <img th:src="@{/img/logo.png}"> |
| th:action | 表单提交地址 | <form th:action="@{/submit}"> |
二、SpringBoot 整合 SpringMVC 自动配置知识点
SpringBoot 通过WebMvcAutoConfiguration自动装配 SpringMVC 全套组件,遵循约定大于配置 ,无需编写 XML,官方文档参考:https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#boot-features-spring-mvc
1. SpringMVC 核心组件自动管理
1 DispatcherServlet(中央转发器)
- 传统 SSM 需在
web.xml手动注册,SpringBoot 完全自动装配; - 自动配置类:
DispatcherServletAutoConfiguration; - 默认拦截路径
/,拦截所有请求(不拦截 JSP),项目无需web.xml文件。
2 Controller 控制器
所有标注@Controller/@RestController的类,只要在 Spring 注解扫描包内,会被容器自动管理,无需额外配置。
3 视图解析器自动装配
- 自动注入
ContentNegotiatingViewResolver(组合视图解析器,整合所有视图解析规则)+BeanNameViewResolver; - 抛弃传统
InternalResourceViewResolver的 XML 配置,Thymeleaf 依靠自身前缀 / 后缀规则完成视图跳转; - 文件上传组件
MultipartResolver自动配置,无需手动注册,默认单文件最大 10MB,可在配置文件修改限制大小。
4 消息转换器 HttpMessageConverter
SpringBoot 内置全套消息转换器,包含字节、字符串、资源、Jackson 序列化转换器等,用于请求 / 响应数据序列化与反序列化,支持扩展 FastJSON 替换默认 Jackson。
5 格式化与转换器 Formatter
容器自动注册内置类型转换器(数字、字符串、日期互转),支持自定义 Formatter 完成日期、特殊格式数据解析,可全局配置默认日期格式yyyy-MM-dd。
6 静态资源与欢迎页
- 静态资源 4 个默认存放目录:
classpath:/META-INF/resources/、classpath:/resources/、classpath:/static/、classpath:/public/; - 访问
localhost:8080/自动匹配静态目录下index.html作为欢迎页; - 自动支持
favicon.ico网站图标映射。
2. SpringMVC 扩展方案(不覆盖自动配置)
若需要自定义拦截器、格式化器、视图跳转、消息转换器,实现WebMvcConfigurer接口 ,添加@Configuration注解,重写对应方法,不会破坏 SpringBoot 默认 MVC 配置。 接口核心扩展方法:
addViewControllers:无 Controller 接口,直接映射 URL 到页面;addFormatters:自定义类型格式化器;configureMessageConverters:自定义消息转换器(如 FastJSON);addInterceptors:注册自定义拦截器,可配置放行 / 拦截路径;addCorsMappings:全局跨域配置。
3. Servlet 三大组件注册方式(Servlet/Filter/Listener)
SpringBoot 嵌入式 Tomcat 无web.xml,通过注册 Bean 的方式配置三大组件:
- Servlet:
ServletRegistrationBean - Filter:
FilterRegistrationBean - Listener:
ServletListenerRegistrationBean
4. 嵌入式容器与外置 Tomcat 区别
- 嵌入式 Tomcat(默认打包 jar)
- 优点:打包简单、一键启动、无需外置服务器;
- 缺点:原生不支持 JSP,容器定制成本高;
- 外置 Tomcat(打包 war)
- 改造步骤:pom 打包方式改为 war、tomcat 依赖 scope 设为 provided、新增
SpringBootServletInitializer子类、配置 webapp 目录; - 启动逻辑:外置 Tomcat 启动,自动执行
SpringBootServletInitializer初始化 Spring 容器。
- 改造步骤:pom 打包方式改为 war、tomcat 依赖 scope 设为 provided、新增
三、总结
- Thymeleaf 记忆点:4 种标准表达式、全套 th 标签、templates 页面约定路径;
- SpringMVC 自动配置:DispatcherServlet、视图解析器、消息转换器全部自动装配,扩展必须实现
WebMvcConfigurer; - SpringBoot Web 两大资源规则:静态资源 4 个目录、Thymeleaf 模板 templates 目录;
- 容器两种部署模式:jar 内置 Tomcat、war 外置 Tomcat,适配不同业务场景。