URL連接有如下幾種類型: html
讓咱們來使用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
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型不是true就是false:spa
<div th:if="${user.isAdmin()} == false"> ...
注意,在上面的例子中,= = false寫在括號外,所以是Thymeleaf自己負責解析解析它。若是是寫在括號內,它將由OGNL負責解析:code
<div th:if="${user.isAdmin() == false}"> ...
<div th:if="${variable.something} == null"> ...
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} > 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>
雙下劃線能夠用\_\_轉義哦!