thymeleaf

Thymeleaf是什麼

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}將返回⼀個名爲xservlet上下⽂屬性。 
spring

標準表達式

簡單表達式

變量表達式$ {...}
選擇變量表達式\* {...}
消息表達式: #{...}
連接⽹址表達式@ {...}
⽚段表達式: 〜{...}

文字

⽂字'one text' , 'Another one!'
數字字⾯值0,34,3.0,12.3...
布爾⽂字truefalse
空字⾯值null
⽂字Tokenonesometextmain...
數組

文本操做:

字符串鏈接+
⽂本替換|The name is ${name}|
服務器

算術運算符

⼆進制運算符+-\*/%
負號(⼀元運算符)-
session

布爾運算符

⼆進制運算符and or
布爾否認(⼀元運算符) : ! , not
app

比較和相等運算符:

⽐較運算符><> =<=gtltgele
相等運算符==, ! =eqne
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')) 

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: 執⾏配置的轉換服務(若是有的話) 的⽅法。
#datesjava.util.Date對象的⽅法: 格式化, 組件提取等
#calendars: 相似於#dates, 但對於java.util.Calendar對象。
#numbers: ⽤于格式化數字對象的⽅法。
#stringsString對象的⽅法: containsstartsWithprepending /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>

經常使用標籤

相關文章
相關標籤/搜索