Thymeleaf教程 (五) Thymeleaf標準表達式語法(下)

URL鏈接

URL鏈接有以下幾種類型:   

  • 絕對地址,如http://www.thymeleaf.org
  • 相對地址 
    • 相對頁面地址.如:/user/login.html
    • 服務器相對地址如:~/billing/processInvoice(部署在同服務器,不同域名的地址)

讓我們來使用th:href屬性:

<!-- Will produce 'http://localhost:8080/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html"
th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

我來解釋下:

  • th:href屬性修飾符:它將計算並替換使用href鏈接URL 值,並放入的href屬性中。
  • 我們可以使用URL參數的表達式(比如在orderId=${o.id} )
  • 如果需要多個參數,這些將由逗號分隔,比如:@{/order/process(execId=${execId},execType=’FAST’)}
  • 變量也允許URL路徑中使用,比如:@{/order/{orderId}/details(orderId=${orderId})}
  • URL中以”/“開頭的路徑(比如/order/details)將會加上服務器地址和域名。形成完整的URL
  • th:href中可以直接使用靜態地址。

URL可以用複雜的表達式:

<a th:href="@{${url}(orderId=${o.id})}">view</a>
<a th:href="@{'/details/'+${user.login}(orderId=${o.id})}">view</a>
  • 1
  • 2

現在我們知道如何創建鏈接的url,那麼添加一個小菜單在我們的網站吧

<p>Please select an option</p>
<ol>
<li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li>
<li><a href="order/list.html" th:href="@{/order/list}">Order List</a></li>
<li><a href="subscribe.html" th:href="@{/subscribe}">Subscribe to our Newsletter</a></li>
<li><a href="userprofile.html" th:href="@{/userprofile}">See User Profile</a></li>
</ol>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

針對同服務器地址,不同域名的URL。可以這樣寫@{~/path/to/something}

基本類型操作

字符型

文本文字可以用單引號來包含。需要轉義的話可以用\’轉義

<p>
Now you are looking at a <span th:text="'working web application'">template file</span>.
</p>
  • 1
  • 2
  • 3

數值型

數值型操作簡單。如下所示:

<p>The year is <span th:text="2013">1492</span>.</p>
<p>In two years, it will be <span th:text="2013 + 2">1494</span>.</p>
  • 1
  • 2

Boolean型

boolean型不是true就是false:

<div th:if="${user.isAdmin()} == false"> ...
  • 1

注意,在上面的例子中,= = false寫在括號外,因此是Thymeleaf本身負責解析解析它。如果是寫在括號內,它將由OGNL負責解析:

<div th:if="${user.isAdmin() == false}"> ...
  • 1

Null型

<div th:if="${variable.something} == null"> ...
  • 1

Literal tokens(不明白什麼意思,大概是字符串文本)

Numeric, boolean 和 null都是字符串文本的一種類型。

只是使表達式更加簡潔。工作時終將解析成字符串文本(‘。。。。。。’),但是他們有更多的限制,比如只能用數字(0~9),下劃線,.,沒有空格,沒有逗號等等。

所以當我們僅僅用字符串的話,可以用這種:

<div th:class="content">...</div>
  • 1

替換掉

<div th:class="'content'">...</div>
  • 1

文本間連接

th:text="'The name of the user is ' + ${user.name}"
  • 1

高級文本連接用法

我們可以用“|”包含住想要連接的文本,替換’…’ + ‘…’方式,這樣就可以省心不少。

<span th:text="|Welcome to our application, ${user.name}!|">
  • 1

替換原來的

<span th:text="'Welcome to our application, ' + ${user.name} + '!'">
  • 1

高級點可以這樣

<span th:text="${onevar} + ' ' + |${twovar}, ${threevar}|">
  • 1

注意:${…}表達式可以被放在|….|之間,但是不能放在’….’之間哦

算術運算

也可以用一些算術運算符:+ , - , * , / , % .

th:with="isEven=(${prodStat.count} % 2 == 0)"
  • 1

比較與相等

 > , < , >= ,<=,== 和 !=都可以用,但是<,>這兩個在必須轉義。

th:if="${prodStat.count} &gt; 1"
th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')"
  • 1
  • 2

當然嫌轉義什麼的太麻煩小朋友,可以用別名替代 gt ( > ), lt ( < ), ge ( >= ), le ( <= ), not ( ! ), eq ( == ),neq / ne ( != ).

條件表達式

可以這樣用

<tr th:class="${row.even}? 'even' : 'odd'">
...
</tr>
  • 1
  • 2
  • 3

也可以這樣中

<tr th:class="${row.even}? (${row.first}? 'first' : 'even') : 'odd'">
...
</tr>
  • 1
  • 2
  • 3

可以省略false的返回值,當然如果false那麼返回的是一個空值

<tr th:class="${row.even}? (${row.first}? 'first' : 'even') : 'odd'">
...
</tr>
  • 1
  • 2
  • 3

默認表達式

默認表達式可以簡化表達式,個人不建議用,閱讀性差。如:

<div th:object="${session.user}">
...
<p>Age: <span th:text="*{age}?: '(no age specified)'">27</span>.</p>
</div>
  • 1
  • 2
  • 3
  • 4

解釋一下:age如果是null的話就執行’(no age specified)’這段,否則就顯示age。跟以下一樣:

<p>Age: <span th:text="*{age != null}? *{age} : '(no age specified)'">27</span>.</p>
  • 1

還可以嵌套玩:

<p>
Name:
<span th:text="*{firstName}?: (*{admin}? 'Admin' : #{default.username})">Sebastian</span>
</p>
  • 1
  • 2
  • 3
  • 4

預處理表達式

有的時候我們需要預處理一些信息到表達式中。比如某個變量的名字是變的,怎麼辦?預處理來了。

預處理表達式用 __${expression}__ 雙下劃線包裹,舉個栗子: 
我們在外部資源文件中配了這個屬性:

article.text=@myapp.translator.Translator@translateToSpanish({0})
  • 1

我們可以在模板中表達式是這樣子的:

<p th:text="${__#{article.text('textVar')}__}">Some text here...</p>
  • 1

那麼引擎會首先從資源文件中獲取article.text的值,再執行它。

<p th:text="${@myapp.translator.Translator@translateToFrench(textVar)}">Some text here...</p>
  • 1

雙下劃線可以用\_\_轉義哦!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章