Thymeleaf是⾯向Web和独⽴环境的现代服务器端Java模板引擎, 可以处理HTML, XML, JavaScript, CSS甚⾄纯⽂本。
html
<html xmlns:th="http://www.thymeleaf.org"> java
$ {x}将返回存储在Thymeleaf上下⽂中的变量x或做为请求属性。
$ {param.x}将返回⼀个名为x的请求参数(多是多值的) 。
$ {session.x}将返回⼀个名为x的会话属性。
$ {application.x}将返回⼀个名为x的servlet上下⽂属性。 spring
⽂字: 'one text' , 'Another one!'
数字字⾯值: 0,34,3.0,12.3, ...
布尔⽂字: true, false
空字⾯值: null
⽂字Token: one, sometext, main, ...数组
字符串链接: +
⽂本替换: |The name is ${name}|服务器
⼆进制运算符: +, -, \*, /, %
负号(⼀元运算符) : -session
⼆进制运算符: and 、 or
布尔否认(⼀元运算符) : ! , notapp
⽐较运算符: >, <, > =, <=(gt, lt, ge, le)
相等运算符: ==, ! =(eq, ne)less
If-then:\(if\) ? \(then\)
If-then-else:\(if\) ? \(then\) : \(else\)
Default:\(value\) ?: \(defaultvalue\)ui
哑操做符: \_spa
全部这些功能能够组合和嵌套:
'User is of type ' + (${user.isAdmin()} ? 'Administrator': (${user.type} ?: 'Unknown'))
<p th:text="#{home.welcome}">Welcome to our grocery store!</p>
spring:
messages:
basename: i18n/hello
<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>
<!-- 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>
<a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
<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>
<!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>