咱們已經知道了兩種語法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
簡單表示式:算法
文字類型:express
條件語句:apache
全部上面算法均可以隨意組合和嵌套: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有如下基本內置對象
Established locale country: <span th:text="${#locale.country}">US</span>.
除了這些基本的對象,Thymeleaf將爲咱們提供一套實用的對象。來幫助咱們咱們執行常見的任務。
如今咱們知道了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>