SpringBoot默认不支持 JSP,需要引入第三方模板引擎技术实现页面渲染。
Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, Javascript, CSS and even plain text.
即 thymeleaf 是现代化、服务端的Java模板引擎。
2. 基本语法
表达式
| 表达式名 | 语法 | 作用 |
|---|---|---|
| 变量取值 | ${…} | 获取请求域、session域、对象等值 |
| 选择变量 | *{…} | 获取上下文对象值 |
| 消息 | #{…} | 获取国际化等值 |
| 链接 | @{…} | 生成链接 |
| 片段表达式 | ~{…} | jsp:include 作用,引入公共页面片段 |
| 行内表达式 | [[…]] | HTML 文本中嵌套表达式 |
字面量
文本值:‘one text’ , ‘another one’
数字:0 , 34 , 3.0 , 12.3
布尔值:true , false
空值:null
变量:one,two
文本操作
字符串拼接: +
变量替换: |The name is ${name}|
数字运算
运算符: + , - , * , / , %
布尔运算
运算符: and , or
一元运算: ! , not
比较运算
比较: > , < , >= , <= ( gt , lt , ge , le )
等式: == , != ( eq , ne )
条件运算
If-then: (if) ?(then)
If-then-else: (if) ? (then) :(else)
Default: (value) ? :(defaultvalue)
特殊操作
无操作: _
3. 标签th:attr
th:attr 用来设置属性值。
设置单个值:
设置多个值:
以上两个的代替写法:




