Thymeleaf教程 (四) Thymeleaf標準表示式語法(上)
阿新 • • 發佈:2019-02-05
我們已經知道了兩種語法
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
<p>Today is: <span th:text="${today}">13 february 2011</span></p>
但是還有很多語法我們不知道,接下來我們快速的介紹更多的表示式語法:
簡單表示式:
- 變量表達式: ${…}
- 選擇變量表達式: *{…}
- 資訊表示式: #{…}
- 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'))
資訊表示式
我們之前的例子是這樣的
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p >
home.welcome=歡迎光臨本店,
但是有的時候我們需要在訊息中增加變數,比如客人的名字怎麼辦?比如達到如下效果
<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有以下基本內建物件
- #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>