Thymeleaf教程 (四) Thymeleaf標準表達式語法(上)

咱們已經知道了兩種語法html

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
<p>Today is: <span th:text="${today}">13 february 2011</span></p>

可是還有不少語法咱們不知道,接下來咱們快速的介紹更多的表達式語法:java

  • 簡單表示式:算法

    • 變量表達式: ${…}
    • 選擇變量表達式: *{…}
    • 信息表達式: #{…}
    • URL鏈接表達式: @{…}
  • 文字類型:express

    • 字符型: ‘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 )
  • 條件語句:apache

    • If-then: (if) ? (then)
    • If-then-else: (if) ? (then) : (else)
    • Default: (value) ?: (defaultvalue)

    全部上面算法均可以隨意組合和嵌套:session

'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))

信息表達式

咱們以前的例子是這樣的app

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
home.welcome=歡迎光臨本店,

可是有的時候咱們須要在消息中增長變量,好比客人的名字怎麼辦?好比達到以下效果ide

<p>¡Bienvenido a nuestra tienda de comestibles, 木魚!</p>

這樣辦:函數

home.welcome=歡迎光臨本店, {0}!
<p th:utext="#{home.welcome(${session.user.name})}">
¡Bienvenido a nuestra tienda de comestibles, 木魚!
</p>

在這裏,參數能夠是字符型也但是樹數值型或者日期型。固然若是咱們須要多個參數的話,類推便可,而且咱們也能夠內嵌表達式替換字符串,好比:工具

<p th:utext="#{${welcomeMsgKey}(${session.user.name})}">
Welcome to our grocery store, 木魚!
</p>

變量表達式

變量表達式能夠解析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>.

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());

替換成

WebContext ctx = new WebContext(request, servletContext, request.getLocale());
ctx.setVariable("today", Calendar.getInstance());
templateEngine.process("home", ctx, response.getWriter());

而後是模板

<p>
Today is: <span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span>
</p>

選擇表達式用法(*{ })

變量不只能用在#{ }上,還能用在* { }上。二者的區別在於* { }上的的變量首先是選定對象的變量。若是不選定對象,那麼是整個上下文環境中的變量和#{ }相同

選擇對象用什麼呢?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>

這個用法等同於

<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>

固然,兩種用法能夠混合。

<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>

若是一個對象已經被選擇,即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>

就像以前說的,若是沒有對象被選中,那麼#{ }和* { }表達式的意義是相同的。

<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>
相關文章
相關標籤/搜索