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>