我們已經知道了兩種語法
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
<p>Today is: <span th:text="${today}">13 february 2011</span></p>
- 1
- 2
但是還有很多語法我們不知道,接下來我們快速的介紹更多的表達式語法:
-
簡單表示式:
- 變量表達式: ${…}
- 選擇變量表達式: *{…}
- 信息表達式: #{…}
- URL連接表達式: @{…}
-
文字類型:
- 字符型: ‘one text’ , ‘Another one!’ ,…
- 數值型: 0 , 34 , 3.0 , 12.3 ,…
- Boolean型: true , false
- 空值: null
- 文本字符串: one , sometext , main ,…
- 字符串操作:
- 字符串連接: +
- 文字替換: |The name is ${name}|
- 數值型操作:
- 運算符: + , - , * , / , %
- 負號: -
- Boolean操作:
- 運算符: and , or
- 非運算符: ! , not
- 比較相等算法:
- 比較: > , < , >= , <= ( gt , lt , ge , le )
- 相等算法: == , != ( eq , ne )
-
條件語句:
- If-then: (if) ? (then)
- If-then-else: (if) ? (then) : (else)
- Default: (value) ?: (defaultvalue)
所有上面算法都可以隨意組合和嵌套:
'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))
- 1
信息表達式
我們之前的例子是這樣的
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
- 1
home.welcome=歡迎光臨本店,
- 1
但是有的時候我們需要在消息中增加變量,比如客人的名字怎麼辦?比如達到如下效果
<p>¡Bienvenido a nuestra tienda de comestibles, 木魚!</p>
- 1
這樣辦:
home.welcome=歡迎光臨本店, {0}!
- 1
<p th:utext="#{home.welcome(${session.user.name})}">
¡Bienvenido a nuestra tienda de comestibles, 木魚!
</p>
- 1
- 2
- 3
在這裏,參數可以是字符型也可是樹數值型或者日期型。當然如果我們需要多個參數的話,類推即可,並且我們也可以內嵌表達式替換字符串,比如:
<p th:utext="#{${welcomeMsgKey}(${session.user.name})}">
Welcome to our grocery store, 木魚!
</p>
- 1
- 2
- 3
變量表達式
變量表達式可以解析OGNL語法。詳盡的語法信息可以訪問官網:
http://commons.apache.org/ognl/
系統基本對象
OGNL有以下基本內置對象
- #ctx : the context
- #object. vars: the context variables.
- #locale : the context locale.
- #httpServletRequest : (only in Web Contexts)theHttpServletRequest object.
- #httpSession : (only in Web Contexts) the HttpSession object.
所以我們可以用如下方式引用:
Established locale country: <span th:text="${#locale.country}">US</span>.
- 1
Thymeleaf提供的對象
除了這些基本的對象,Thymeleaf將爲我們提供一套實用的對象。來幫助我們我們執行常見的任務。
- #dates : 爲 java.util.Date對象提供工具方法,比如:格式化,提取年月日等.
- #calendars : 類似於#dates , 但是隻針對java.util.Calendar對象.
- #numbers : 爲數值型對象提供工具方法。
- #strings :爲String 對象提供工具方法。如: contains, startsWith, prepending/appending等。
- #objects : 爲object 對象提供常用的工具方法。
- #bools : 爲boolean 對象提供常用的工具方法。
- #arrays : 爲arrays 對象提供常用的工具方法。
- #lists :爲lists對象提供常用的工具方法。
- #sets : 爲sets對象提供常用的工具方法。
- #maps : 爲maps對象提供常用的工具方法。
- #aggregates :爲創造一個arrays 或者 collections聚集函數提供常用的工具方法。
- #messages : utility methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax?.
- #ids : 爲可能需要循環的ID屬性提供常用的工具方法。
在我們的主頁中重新格式化日期
現在我們知道了Thymeleaf提供的工具類和表達式的語法,那麼我們來重新格式化首頁的日期吧,首先在我們的controller層中吧字符型日期替換成對象
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
Calendar cal = Calendar.getInstance();
WebContext ctx = new WebContext(request, servletContext, request.getLocale());
ctx.setVariable("today", dateFormat.format(cal.getTime()));
templateEngine.process("home", ctx, response.getWriter());
- 1
- 2
- 3
- 4
- 5
替換成
WebContext ctx = new WebContext(request, servletContext, request.getLocale());
ctx.setVariable("today", Calendar.getInstance());
templateEngine.process("home", ctx, response.getWriter());
- 1
- 2
- 3
然後是模板
<p>
Today is: <span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span>
</p>
- 1
- 2
- 3
選擇表達式用法(*{ })
變量不僅能用在#{ }上,還能用在* { }上。兩者的區別在於* { }上的的變量首先是選定對象的變量。如果不選定對象,那麼是整個上下文環境中的變量和#{ }相同
選擇對象用什麼呢?th:object標籤屬性。我們使用它在我們的用戶配置文件(userprofile.html)頁面:
<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>
- 1
- 2
- 3
- 4
- 5
這個用法等同於
<div>
<p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>
</div>
- 1
- 2
- 3
- 4
- 5
當然,兩種用法可以混合。
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
- 1
- 2
- 3
- 4
- 5
如果一個對象已經被選擇,即th:object=”${session.user}”。那麼我們也使用#object對象去引用。
<div th:object="${session.user}">
<p>Name: <span th:text="${#object.firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
- 1
- 2
- 3
- 4
- 5
就像之前說的,如果沒有對象被選中,那麼#{ }和* { }表達式的意義是相同的。
<div>
<p>Name: <span th:text="*{session.user.name}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{session.user.surname}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{session.user.nationality}">Saturn</span>.</p>
</div>
- 1
- 2
- 3
- 4
- 5