thymeleaf
Thymeleaf是什么
Thymeleaf是⾯向Web和独⽴环境的现代服务器端Java模板引擎, 能够处理HTML, XML, JavaScript, CSS甚⾄纯⽂本。
<html xmlns:th="http://www.thymeleaf.org">
获取属性
$ {x}将返回存储在Thymeleaf上下⽂中的变量x或作为请求属性。
$ {param.x}将返回⼀个名为x的请求参数(可能是多值的) 。
$ {session.x}将返回⼀个名为x的会话属性。
$ {application.x}将返回⼀个名为x的servlet上下⽂属性。
标准表达式
简单表达式:
变量表达式: $ {...}
选择变量表达式: \* {...}
消息表达式: #{...}
链接⽹址表达式: @ {...}
⽚段表达式: 〜{...}
文字
⽂字: 'one text' , 'Another one!'
数字字⾯值: 0,34,3.0,12.3, ...
布尔⽂字: true, false
空字⾯值: null
⽂字Token: one, sometext, main, ...
文本操作:
字符串连接: +
⽂本替换: |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\)
特殊符号:
哑操作符: \_
所有这些功能可以组合和嵌套:
'User is of type ' + (${user.isAdmin()} ? 'Administrator': (${user.type} ?: 'Unknown'))
th:text
<p th:text="#{home.welcome}">Welcome to our grocery store!</p>
spring:
messages:
basename: i18n/hello
th:utext
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
⼯具表达式对象
#execInfo: 有关正在处理的模板的信息。
#messages: ⽤于在变量表达式中获取外部化消息的⽅法, 与使⽤#{...}语法获得的⽅式相同。
#uris: 转义URL / URI部分的⽅法
#conversions: 执⾏配置的转换服务(如果有的话) 的⽅法。
#dates: java.util.Date对象的⽅法: 格式化, 组件提取等
#calendars: 类似于#dates, 但对于java.util.Calendar对象。
#numbers: ⽤于格式化数字对象的⽅法。
#strings: String对象的⽅法: contains, startsWith, prepending /appending等
#objects: ⼀般对象的⽅法。
#bools: 布尔评估的⽅法。
#arrays: 数组的⽅法。
#lists: 列表的⽅法。
#sets: 集合的⽅法。
#maps: 地图⽅法。
#aggregates: 在数组或集合上创建聚合的⽅法。
#ids: 处理可能重复的id属性的⽅法(例如, 作为迭代的结果) 。
星号语法
<div th:object="${session.user}"> <p>Name: <span th:text="*{firstName}">Sebastian</span></p> <p>Surname: <span th:text="*{lastName}">Pepper</span></p> <p>Nationality: <span th:text="*{nationality}">Saturn</span></p> </div>
URL链接
<!-- Will produce 'http://localhost:8080/gtvg/order/detail s?orderId=3' (plus rewriting) --> <a href="details.html" th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a> <!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) --> <a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a> <!-- Will produce '/gtvg/order/3/details' (plus rewriting)--> <a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
⽂本替换
<span th:text="|Welcome to our application, ${user.name}!|">
哑操作符号
<span th:text="${user.name} ?: _">no user authenticated</span>
if/unless
<a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
switch
<div th:switch="${user.role}"> <p th:case="'admin'">User is an administrator</p> <p th:case="#{roles.manager}">User is a manager</p> <p th:case="*">User is some other thing</p> </div>
th:each
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>hello</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <!-- 不存在则忽略,显示hello null!(可以通过默认值进行设置)--> <p th:text="'Hello ' + (${name}?:'admin')">3333</p> <table> <tr> <th>ID</th> <th>NAME</th> <th>AGE</th> </tr> <tr th:each="emp : ${empList}"> <td th:text="${emp.id}">1</td> <td th:text="${emp.name}">海</td> <td th:text="${emp.age}">18</td> </tr> </table> </body> </html>
常用标签