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

URL連接

URL連接有如下幾種類型:   html

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

讓咱們來使用th:href屬性:web

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

我來解釋下:express

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

如今咱們知道如何建立連接的url,那麼添加一個小菜單在咱們的網站吧session

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

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

基本類型操做

字符型

文本文字能夠用單引號來包含。須要轉義的話能夠用\’轉義網站

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

數值型

數值型操做簡單。以下所示:url

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

Boolean型

boolean型不是true就是false:spa

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

注意,在上面的例子中,= = false寫在括號外,所以是Thymeleaf自己負責解析解析它。若是是寫在括號內,它將由OGNL負責解析:code

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

Null型

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

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

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

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

因此當咱們僅僅用字符串的話,能夠用這種:

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

替換掉

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

文本間鏈接

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

高級文本鏈接用法

咱們能夠用「|」包含住想要鏈接的文本,替換’…’ + ‘…’方式,這樣就能夠省心很多。

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

替換原來的

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

高級點能夠這樣

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

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

算術運算

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

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

比較與相等

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

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

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

條件表達式

能夠這樣用

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

也能夠這樣中

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

能夠省略false的返回值,固然若是false那麼返回的是一個空值

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

默認表達式

默認表達式能夠簡化表達式,我的不建議用,閱讀性差。如:

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

解釋一下:age若是是null的話就執行’(no age specified)’這段,不然就顯示age。跟如下同樣:

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

還能夠嵌套玩:

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

預處理表達式

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

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

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

咱們能夠在模板中表達式是這樣子的:

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

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

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

雙下劃線能夠用\_\_轉義哦!

相關文章
相關標籤/搜索