Thymleaf是一個Java庫旋廷,之是XML、XHTML醒陆、HTML5等格式的模板引擎,可用于Web項(xiàng)目和非Web項(xiàng)目裆针。
Thymleaf很適合作為Web應(yīng)用的視圖的業(yè)務(wù)邏輯層刨摩,還可以在離線環(huán)境下處理XML語言。
Thyleaf可作為SpringMVC的可選模塊据块,也可以直接代替JSP码邻。
其主要目標(biāo)是提供一種可被瀏覽器正確顯示的、格式良好的模板創(chuàng)建方式另假,因此也可以用作靜態(tài)建模像屋。
首先需要在html頁面加入頭文件生意人schema這個網(wǎng)址
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
其次將thymleaf標(biāo)簽替換原始模板以及樣式。
依賴:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>2.1.4</version>
</dependency>
整合Spring:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>2.1.4</version>
</dependency>
thymeleaf 的context边篮,即提供數(shù)據(jù)的地方己莺,基于web的context奏甫,即WebContext相對context增加 param,session,application變量,并且自動將request atttributes添加到context variable map凌受,可以在模板直接訪問阵子。
在模板處理前,thymeleaf還會增加一個變量execInfo胜蛉,比如${execInfo.templateName},${execInfo.now}等挠进。
數(shù)據(jù)訪問模式:
${...},變量引用模式誊册,比如${myBean.property}领突,如果用springDialect,則使用的是spring EL案怯,如果不用spring君旦,則用的ognl。
{...}嘲碱,選擇表達(dá)式金砍,一般是th:object之后,直接取object中的屬性麦锯。當(dāng)沒有選取對象時恕稠,其功能等同${...},{firstName}也等同于${#object.firstName},#object代表當(dāng)前選擇的對象。
@{...}鏈接url的表達(dá)式离咐。th:href="@{/xxx/aa.do(id=${o.id})"谱俭,會自動進(jìn)行url-encoding的處理。@{...}內(nèi)部可以是需要計算的表達(dá)式宵蛀,比如:th:href=”@{'/details/'+${user.login}(orderId=${o.id})}"
{...},i18n县貌,國際化术陶。
需要注意的:#{${welcomeMsgKey}(${session.user.name})}:i18n message支持占位。各個表達(dá)式支持嵌套煤痕。
表達(dá)式基本對象:
ctx:context object
root或者#vars
locale
httpServletRequest
httpSession
表達(dá)式功能對象:
dates:java.util.Date的功能方法類梧宫。
calendars:類似#dates,面向java.util.Calendar#numbers:格式化數(shù)字的功能方法類摆碉。
strings:字符串對象的功能類塘匣,contains,startWiths,prepending/appending等等。
objects:對objects的功能類操作巷帝。
bools:對布爾值求值的功能方法忌卤。
arrays:對數(shù)組的功能類方法。
lists:對lists功能類方法
sets
maps
aggregates:對數(shù)組或者集合創(chuàng)建聚合的功能方th:text="${#aggregates.sum(o.orderLines.{purchasePrice * amount})}"
messages:在變量表達(dá)式中獲取外部信息的功能類方法楞泼。
ids:處理可能重復(fù)的id屬性的功能類方法驰徊。
條件操作:
(if)?(then):滿足條件笤闯,執(zhí)行then。
(if)?(then):(else)(value)?:(defalutValue)
一些標(biāo)簽:
th:text="${data}",將data的值替換該屬性所在標(biāo)簽的body棍厂。字符常量要用引號颗味,比如th:text="'hello world'",th:text="2011+3",th:text="'my name is '+${user.name}"
th:utext,和th:text的區(qū)別是"unescaped text"牺弹。
th:with,定義變量浦马,th:with="isEven=${prodStat.count}%2==0",定義多個變量可以用逗號分隔张漂。
th:attr晶默,設(shè)置標(biāo)簽屬性,多個屬性可以用逗號分隔鹃锈,比如th:attr="src=@{/image/aa.jpg},title=#{logo}"荤胁,此標(biāo)簽不太優(yōu)雅,一般用的比較少屎债。
th:[tagAttr],設(shè)置標(biāo)簽的各個屬性仅政,比如th:value,th:action等∨杈裕可以一次設(shè)置兩個屬性圆丹,比如:th:alt-title="#{logo}"對屬性增加前綴和后綴,用th:attrappend躯喇,th:attrprepend,比如:th:attrappend="class=${' '+cssStyle}"對于屬性是有些特定值的辫封,比如checked屬性,thymeleaf都采用bool值廉丽,比如th:checked=${user.isActive}
th:each, 循環(huán)倦微,<tr th:each="user,userStat:${users}">,userStat是狀態(tài)變量,有 index,count,size,current,even,odd,first,last等屬性正压,如果沒有顯示設(shè)置狀態(tài)變量欣福,thymeleaf會默 認(rèn)給個“變量名+Stat"的狀態(tài)變量。
th:if or th:unless焦履,條件判斷拓劝,支持布爾值,數(shù)字(非零為true)嘉裤,字符郑临,字符串等。
th:switch屑宠,th:case厢洞,選擇語句。 th:case="*"表示default case。
th:fragment犀变,th:include,th:substituteby:fragment為片段標(biāo)記妹孙,指定一個模板內(nèi)一部分代碼為一個片段,然后在其它的頁面中用th:include或th:substituteby進(jìn)行包含获枝。包含的格式為蠢正,格式內(nèi)可以為表達(dá)式,比如th:include="footer::$(user.logined)?'logined':'notLogin'":"templatename::fragname"省店,指定模板內(nèi)的指定片段嚣崭。"templateName::[domselector]",指定模板的dom selector懦傍,被包含的模板內(nèi)不需要th:fragment.”templatename"雹舀,包含整個模板。
th:include和th:substituteby的區(qū)別在于前者包含片段的內(nèi)容到當(dāng)前標(biāo)簽內(nèi)粗俱,后者是用整個片段(內(nèi)容和上一層)替換當(dāng)前標(biāo)簽(不僅僅是標(biāo)簽內(nèi)容)说榆。
th:remove="all|body|tag|all-but-first",一般用于將mock數(shù)據(jù)在真實(shí)環(huán)境中移除寸认,all表示移除標(biāo)簽以及標(biāo)簽內(nèi)容签财,body只移除內(nèi)容,tag只移除所屬標(biāo)簽偏塞,不移除內(nèi)容唱蒸,all-but-first,除第一條外其它不移除灸叼。
由 于一個標(biāo)簽內(nèi)可以包含多個th:x屬性神汹,其先后順序?yàn)椋篿nclude,each,if/unless/switch/case,with,attr /attrprepend/attrappend,value/href,src ,etc,text/utext,fragment,remove。
官網(wǎng)文檔:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls
借鑒文章:http://www.blogjava.net/bjwulin/archive/2013/02/07/395234.html