thymeleaf 語法
一、語法:
1、 簡單表達式 (simple expressions)
${...} 變量表達式
*{...} 選擇變量表達式
#{...} 消息表達式
@{...} 鏈接url表達式
2、字面量
‘one text‘,‘another one!‘,... 文本
0,34,3.0,12.3,... 數值
true false 布爾類型
null 空
one,sometext,main 文本字符
3、 文本操作
+ 字符串連接
|The name is ${name}| 字符串連接
4、算術運算
+ , - , * , / , % 二元運算符
- 負號(一元運算符)
5、布爾操作
and,or 二元操作符
!,not 非(一元操作符)
6、關系操作符
> , < , >= , <= (gt , lt , ge , le)
== , != (eq, ne)
7、條件判斷
(if) ? (then) if-then
(if) ? (then) : (else) if-then-else
<tr th:class="${row.even}? ‘even‘ : ‘odd‘"> ... </tr>
條件表達式中的三個部分自身也可以是表達式,也可以是變量(${...}, *{...}), 消息(#{...}), URL (@{...}) 或字面量 (‘...‘)
<tr th:class="${row.even}? (${row.first}? ‘first‘ : ‘even‘) : ‘odd‘"> ... </tr>
else表達式也可以省略,當條件為false時,會返回null:
<tr th:class="${row.even}? ‘alt‘"> ... </tr>
(value) ?: (defaultvalue) Default
只有在第一個表達式返回null時,第二個表達式才會運算
·表達式工具對象
- #dates 與java.util.Date對象的方法對應,格式化、日期組件抽取等等
- #calendars 類似#dates,與java.util.Calendar對象對應
- #numbers 格式化數字對象的工具方法
- #strings 與java.lang.String對應的工具方法:contains、startsWith、prepending/appending等等
- #objects 用於對象的工具方法
- #bools 用於布爾運算的工具方法
- #arrays 用於數組的工具方法
- #lists 用於列表的工具方法
- #sets 用於set的工具方法
- #maps 用於map的工具方法
- #aggregates 用於創建數組或集合的聚合的工具方法
- #messages 用於在變量表達式內部獲取外化消息的工具方法,與#{…}語法獲取的方式相同
- #ids 用於處理可能重復出現(例如,作為遍歷的結果)的id屬性的工具方法
8、鏈接URL
URL在web模板中是一級重要元素,使用@{…}表示
URL的類型:
絕對URL:
- http://www.thymeleaf.org
相對URL:
- 頁面相對: /user/login.html
- 上下文相對:/itemdetails?id=3 (服務器上下文名稱會被自動添加)
- 服務器相對:~/billing/processInvoice(允許調用同一服務器上的另一個上下文中的URL)
- 協議相對://code.jquery.com/jquery-2.0.3.min.js
Thymeleaf在任何情況下都可以處理絕對URL,對於相對URL,則需要使用一個實現了IWebContext接口的上下文對象,這個對象包含了來自HTTP請求的信息,這些信息用於創建相對鏈接。
<!-- Will produce ‘http://localhost:8080/gtvg/order/details?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>
9、 預處理
Thymeleaf提供預處理表達式的功能。
它是在表殼式正常執行前執行的操作,允許修改最終將要被執行的表達式。
預處理表達式跟正常的一樣,但被兩個下劃線包圍住,例如:__${expression}__
假設有一個i18n消息文件Message_fr.properties,裏面有一個條目包含了一個調用具體語言的靜態方法的OGNL表達式:
[email protected]@translateToFrench({0})
Messages_es.properties中的等價條目:
[email protected]@translateToSpanish({0})
可以根據locale先創建用於運算表達式的標記片段,本例中,先通過預處理選擇表達式,然後讓Thymeleaf處理這個選擇出來的表達式:
<p th:text="${__#{article.text(‘textVar‘)}__}">Some text here...</p>
對於locale為French的情況,上面的表達式經過預處理後,得出的等價物如下:
<p th:text="${@myapp.translator.Translator@translateToFrench(textVar)}">Some text here...</p>
二、 設置屬性值
1、th:attr 任何屬性值
<form action="subscribe.html" th:attr="action=@{/subscribe}"> <fieldset> <input type="text" name="email" /> <input type="submit" value="Subscribe me!" th:attr="value=#{subscribe.submit}"/> </fieldset> </form>
2、多個屬性一起設置,用逗號隔開
<img src="../../images/gtvglogo.png" th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />
3、設置指定屬性
th:abbr th:accept th:accept-charset th:accesskey th:action th:align th:alt th:archive th:audio th:autocomplete th:axis th:background th:bgcolor th:border th:cellpadding th:cellspacing th:challenge th:charset th:cite th:class th:classid ...
<input type="submit" value="Subscribe me!" th:value="#{subscribe.submit}"/>
<form action="subscribe.html" th:action="@{/subscribe}">
<li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li>
4、設置多個屬性在同一時間 有兩個特殊的屬性可以這樣設置: th:alt-title 和 th:lang-xmllang
th:alt-title 設置 alt 和 title
th:lang-xmllang 設置 lang 和 xml:lang
<img src="../../images/gtvglogo.png" th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" /> <img src="../../images/gtvglogo.png"th:src="@{/images/gtvglogo.png}" th:title="#{logo}" th:alt="#{logo}" /> <img src="../../images/gtvglogo.png"th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" />
5、前置和後置添加屬性值 th:attrappend 和 th:attrprepend
<input type="button" value="Do it!" class="btn" th:attrappend="class=${‘ ‘ + cssStyle}" />
編譯後:
<input type="button" value="Do it!" class="btn warning" />
6、還有兩個特定的添加屬性 th:classappend 和 th:styleappend
<tr th:each="prod : ${prods}" class="row" th:classappend="${prodStat.odd}? ‘odd‘">
7、修復的布爾屬性
<input type="checkbox" name="active" th:checked="${user.active}" />
所有修復的布爾屬性:
|th:async |th:autofocus |th:autoplay | |th:checked |th:controls |th:declare | |th:default |th:defer |th:disabled | |th:formnovalidate|th:hidden |th:ismap | |th:loop |th:multiple |th:novalidate | |th:nowrap |th:open |th:pubdate | |th:readonly |th:required |th:reversed | |th:scoped |th:seamless |th:selected |
8、HTML5友好的屬性及元素名
<table> <tr data-th-each="user : ${users}"> <td data-th-text="${user.login}">...</td> <td data-th-text="${user.name}">...</td> </tr> </table>
data-{prefix}-{name}是編寫HTML5自定義屬性的標準語法,不需要開發者使用th:*這樣的命名空間,Thymeleaf讓這種語法自動對所有dialect都可用。
三、遍歷
1、基礎
<tr th:each="prod : ${prods}"> <td th:text="${prod.name}">Onions</td> <td th:text="${prod.price}">2.41</td> <td th:text="${prod.inStock}? #{true} : #{false}">yes</td> </tr>
可遍歷的對象:實現java.util.Iterable、java.util.Map(遍歷時取java.util.Map.Entry)、array、任何對象都被當作只有對象自身一個元素的列表
2、狀態
- 當前遍歷索引,從0開始,index屬性
- 當前遍歷索引,從1開始,count屬性
- 總元素數量,size屬性
- 每一次遍歷的iter變量,current屬性
- 當前遍歷是even還是odd,even/odd布爾屬性
- 當前遍歷是第一個,first布爾屬性
- 當前遍歷是最後一個,last布爾屬性
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? ‘odd‘"> <td th:text="${prod.name}">Onions</td> <td th:text="${prod.price}">2.41</td> <td th:text="${prod.inStock}? #{true} : #{false}">yes</td> </tr>
若不指定狀態變量,Thymeleaf會默認生成一個名為“變量名Stat”的狀態變量:
<tr th:each="prod : ${prods}" th:class="${prodStat.odd}? ‘odd‘"> <td th:text="${prod.name}">Onions</td> <td th:text="${prod.price}">2.41</td> <td th:text="${prod.inStock}? #{true} : #{false}">yes</td> </tr>
四、條件運算
<tr th:each="prod : ${prods}" th:class="${prodStat.odd}? ‘odd‘"> <td th:text="${prod.name}">Onions</td> <td th:text="${prod.price}">2.41</td> <td th:text="${prod.inStock}? #{true} : #{false}">yes</td> <td> <span th:text="${#lists.size(prod.comments)}">2</span> comment/s <a href="comments.html" th:href="@{/product/comments(prodId=${prod.id})}" th:if="${not #lists.isEmpty(prod.comments)}">view</a> </td> </tr>
<a href="comments.html" th:href="@{/product/comments(prodId=${prod.id})}" th:if="${not #lists.isEmpty(prod.comments)}">view</a>
1、th:if 不只運算布爾條件,它對以下情況也運算為true:
值不為null
值為boolean且為true
值為數字且非0
值為字符且非0
值是字符串且不是:“false”,“off”,“no”
值不是boolean、數字、字符、字符串
如果值為null,則th:if運算結果為false
2、th:if的反面是th:unless
<a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}">view</a>
3、th:switch 和 th:case
<div th:switch="${user.role}"> <p th:case="‘admin‘">User is an administrator</p> <p th:case="#{roles.manager}">User is a manager</p> </div> <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>
thymeleaf 語法