https://blog.csdn.net/m0_38043362/article/details/78064177
Thymeleaf表達(dá)式語法以及日期格式化
2017年09月22日 16:18:47?試水流連?閱讀數(shù):10693
?版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載命锄。https://blog.csdn.net/m0_38043362/article/details/78064177
簡單表達(dá)式 (simple expressions)
${...}? 變量表達(dá)式
*{...}? 選擇變量表達(dá)式
#{...}? 消息表達(dá)式
@{...}? 鏈接url表達(dá)式
1
2
3
4
5
6
7
字面量
Text literals: 'one text' , 'Another one!' ,…文本
Number literals: 0 , 34 , 3.0 , 12.3 ,…數(shù)字
Boolean literals: true , false布爾
Null literal: null空
Literal tokens: one , sometext , main ,…文本字符
1
2
3
4
5
6
7
8
9
文本操作
字符串連接
|The name is ${name}| 字符串連接
算術(shù)運(yùn)算
+ , - , * , / , %? 二元運(yùn)算符
-? 負(fù)號(一元運(yùn)算符)
1
2
3
布爾操作
and,or 二元操作符
!,not 非(一元操作符)
關(guān)系操作符
, < , >= , <= (gt , lt , ge , le) 比大小
== , != (eq, ne) 比等值
條件判斷
(if) ? (then)? ? ? if-then
(if) ? (then) : (else)? if-then-else?
1
2
3
<!-- 條件判斷 -->
1
2
3
4
條件表達(dá)式中的三個部分自身也可以是表達(dá)式,也可以是變量(${...}, *{...}),
消息(#{...}), URL (@{...}) 或字面量 ('...')
條件表達(dá)式也可以使用括號來嵌套:
1
2
3
<!-- 條件判斷 -->
1
2
3
else表達(dá)式也可以省略,當(dāng)條件為false時廓旬,會返回null:
1
<!-- 條件判斷 -->
1
2
3
(value) ?: (defaultvalue)? Default
只有第一個返回null時第二個表達(dá)式才會運(yùn)算
1
2
表達(dá)式內(nèi)置工具對象
#dates 與java.util.Date對象的方法對應(yīng),格式化哥艇、日期組件抽取等等#calendars 類似#dates,與java.util.Calendar對象對應(yīng)#numbers格式化數(shù)字對象的工具方法#strings 與java.lang.String對應(yīng)的工具方法:contains僻澎、startsWith貌踏、prepending/appending等等#objects 用于對象的工具方法#bools 用于布爾運(yùn)算的工具方法#arrays 用于數(shù)組的工具方法#lists 用于列表的工具方法#sets 用于set的工具方法#maps 用于map的工具方法#aggregates 用于創(chuàng)建數(shù)組或集合的聚合的工具方法#messages 用于在變量表達(dá)式內(nèi)部獲取外化消息的工具方法,與#{…}語法獲取的方式相同#ids 用于處理可能重復(fù)出現(xiàn)(例如窟勃,作為遍歷的結(jié)果)的id屬性的工具方法
1
2
3
4
5
6
7
8
9
10
11
12
13
表達(dá)式內(nèi)置基本對象
#ctx : the context object.
#vars: the context variables.
#locale : the context locale.
#request : (only in Web Contexts) the HttpServletRequest object.
(在web環(huán)境下才可用)
#response : (only in Web Contexts) the HttpServletResponse object.
(在web環(huán)境下才可用)
#session : (only in Web Contexts) the HttpSession object.
(在web環(huán)境下才可用)
#servletContext : (only in Web Contexts) the ServletContext object.
(在web環(huán)境下才可用)
1
2
3
4
5
6
7
8
9
10
11
12
選擇表達(dá)式(*{xx})
<!-- 選擇表達(dá)式 --><!-- 等同于 -->
1
2
3
4
5
6
url連接@{…}
使用這種方式的好處就是可以自動將()內(nèi)的中文參數(shù)自動進(jìn)行URL編碼
<!-- URL --><!-- href="/demo/page?param=%E6%B5%8B%E8%AF%95%E7%94%A8%E6%88%B7%EF%BC%81" -->demo page<!-- href="/" -->demo page<!-- href="/demo/page?param=%E6%B5%8B%E8%AF%95%E7%94%A8%E6%88%B7%EF%BC%81" -->demo page
1
2
3
4
5
6
7
變量表達(dá)式
變量表達(dá)式可以解析OGNL語法祖乳。詳盡的語法信息可以訪問官網(wǎng):?
http://commons.apache.org/ognl/
字面值替換
<!--
替換字面值
<p th:text="|Welcome to our application, ${custUser.nickname}!|">你好</p>
等同于
<p th:text="'Welcome to our application, ' + ${custUser.nickname} + '!'">你好</p>
-->你好
1
2
3
4
5
6
7
數(shù)字運(yùn)算
<!-- 數(shù)字運(yùn)算 -->
1
2
message表達(dá)式#{..}
從配置文件中取配置的信息?
可以通過該表達(dá)式,實(shí)現(xiàn)國際化信息
<!-- 國際化秉氧,將submit按鈕的message從messages_zh_CN.properties(默認(rèn))中獲取 -->
1
2
3
4
5
6
7
屬性設(shè)置
th:attr 為萬能屬性設(shè)置眷昆,內(nèi)容為key value 形式多個屬性用,號分隔
1
2
3
4
5
6
指定單個屬性
1
2
支持的屬性?
參見官方文檔
屬性介紹
<!--
追加屬性 和 前置追加
email is :12222222@xx.com
12222222@xx.com is email.
--><!-- 數(shù)據(jù)回顯時汁咏,使用這個方法來選擇 -->123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
循環(huán)
可以處理如下對象的遍歷?
java.util.Iterable?
java.util.Enumeration?
java.util.Iterator?
java.util.Map?
any array
<!-- 循環(huán)標(biāo)簽 -->NAMEPRICEIN STOCK
? ? ? ? +'? '+ elementStat.count +'? '+ elementStat.size
? ? ? ? +'? '+ elementStat.current +'? '+ elementStat.even
? ? ? ? +'? '+ elementStat.odd +'? '+ elementStat.first
? ? ? ? +'? '+ elementStat.last}">OnionsNAMEPRICEIN STOCK
? ? ? ? +'? '+ iterStat.count +'? '+ iterStat.size
? ? ? ? +'? '+ iterStat.current +'? '+ iterStat.even
? ? ? ? +'? '+ iterStat.odd +'? '+ iterStat.first
? ? ? ? +'? '+ iterStat.last}">Onions<!--
? ? ? ? th:if 條件判斷亚斋,相當(dāng)于if(!(xx==xx)){}
? ? ? ? th:unless 與th:if正好相反
? ? ? ? -->角標(biāo)不為1角標(biāo)為1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
循環(huán)的狀態(tài)屬性 默認(rèn)從element+Stat取值 或者自己制定 前綴+Stat?
index 角標(biāo)從0開始?
count 從1開始?
size 集合大小?
current 當(dāng)前元素?
even/odd 奇偶?
first(boolean)?
last(boolean)
switch語句
<!-- switch標(biāo)簽 -->User is an administratorhello my text!default
1
2
3
4
5
6
7
使用th:case=”*” 相當(dāng)于default:
swich(x){case1:break;case2:break;default:}
1
2
3
4
5
6
7
8
Fragments片段表達(dá)式~{…} 3.0以后的版本支持
1.~{templatename::selector}
可以利用此功能方便地將重復(fù)的片段抽取出來
如footer header
1
2
3
4
<!-- 代碼片段定義在fragment.html中 --><!-- <div th:fragment="copy">
? ? ? 2011 The Good Thymes Virtual Grocery
</div> --><!-- 代碼片段使用其他頁面均可使用該表達(dá)式引入 --><!-- 替換掉當(dāng)前外層div標(biāo)簽 --><!-- 這么寫也可以 -->
1
2
3
4
5
6
7
8
9
10
11
12
2.~{templatename}
包含指定模板中所有內(nèi)容
3.~{::selector}" or "~{this::selector}"
自己頁面內(nèi)的模板可以省略掉模板名稱
1
2
3
4
5
以上的表達(dá)式都支持在selector中支持支持的所有表達(dá)式
1
也可以不使用 th:fragment來定義片段
? 2011 The Good Thymes Virtual Grocery
1
2
3
4
5
可以指定參數(shù)的Fragments片段
<!-- 指定參數(shù)的片段 --><!-- <div th:fragment="frag (onevar,twovar)">
<p th:text="${onevar} + ' - ' + ${twovar}">...</p>
</div> -->
1
2
3
4
5
6
7
Fragments片段支持嵌套
<!-- 指定要嵌套的片段 --><!-- 片段嵌套 -->
no operation
<!-- 片段嵌套 --><!-- 不指定參數(shù) -->1
2
3
4
5
6
7
8
9
10
11
12
這種不能放到一個頁面里,否則會解析不到${otherfrag}
也可以使用表達(dá)式判斷條件決定是否引入
定義局部變量
<!-- 定義局部變量 -->
The name of the first person isJulius Caesar.
1
2
3
4
5
6
行內(nèi)表達(dá)式
<!-- 行內(nèi)表達(dá)式 -->
今日陽光明媚攘滩,[[${custUser.nickname}]]
<!-- 與下面的等價 -->今日陽光明媚帅刊,
1
2
3
4
格式化日期等${{user.lastAccessDate}}
...
1
使用${{xxx}}該表達(dá)式,解析器會根據(jù)符合 xxx類型–>String的轉(zhuǎn)換器漂问,進(jìn)行轉(zhuǎn)換?
例如:
@ConfigurationpublicclassThymeleafConfigextendsWebMvcConfigurerAdapterimplementsApplicationContextAware{....@OverridepublicvoidaddFormatters(finalFormatterRegistry registry) {super.addFormatters(registry);? ? ? ? ? registry.addFormatter(dateFormatter());? ? ? }@BeanpublicDateFormatterdateFormatter() {returnnewMyDateFormatter();? ? ? }? ? ? class MyDateFormatter extends DateFormatter{@OverridepublicStringprint(Date date, Locale locale) {returnnewSimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);? ? ? ? }? ? ? }}