thymeleaf常用教程

URL表達式

URL表達式指的是把一個有用的上下文或回話信息添加到URL,這個過程經常被叫做URL重寫。 
@{/order/list} 
URL還可以設置參數: 
@{/order/details(id=${orderId})} 
相對路徑: 
@{../documents/report}

讓我們看這些表達式:

    <form th:action="@{/createOrder}">  
    <a href="main.html" th:href="@{/main}">

變量表達式和星號表達有什麼區別嗎?

如果不考慮上下文的情況下,兩者沒有區別;星號語法評估在選定對象上表達,而不是整個上下文 
什麼是選定對象?就是父標籤的值,如下:

  <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 th:object="${session.user}">
      <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標籤都有那些?

關鍵字 功能介紹 案例
th:id 替換id <input th:id="'xxx' + ${collect.id}"/>
th:text 文本替換 <p th:text="${collect.description}">description</p>
th:utext 支持html的文本替換 <p th:utext="${htmlcontent}">conten</p>
th:object 替換對象 <div th:object="${session.user}">
th:value 屬性賦值 <input th:value="${user.name}" />
th:with 變量賦值運算 <div th:with="isEven=${prodStat.count}%2==0"></div>
th:style 設置樣式 th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"
th:onclick 點擊事件 th:onclick="'getCollect()'"
th:each 屬性賦值 tr th:each="user,userStat:${users}">
th:if 判斷條件 <a th:if="${userId == collect.userId}" >
th:unless 和th:if判斷相反 <a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
th:href 鏈接地址 <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> />
th:switch 多路選擇 配合th:case 使用 <div th:switch="${user.role}">
th:case th:switch的一個分支 <p th:case="'admin'">User is an administrator</p>
th:fragment 佈局標籤,定義一個代碼片段,方便其它地方引用 <div th:fragment="alert">
th:include 佈局標籤,替換內容到引入的文件 <head th:include="layout :: htmlhead" th:with="title='xx'"></head> />
th:replace 佈局標籤,替換整個標籤到引入的文件 <div th:replace="fragments/header :: title"></div>
th:selected selected選擇框 選中 th:selected="(${xxx.id} == ${configObj.dd})"
th:src 圖片類地址引入 <img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" />
th:inline 定義js腳本可以使用變量 <script type="text/javascript" th:inline="javascript">
th:action 表單提交的地址 <form action="subscribe.html" th:action="@{/subscribe}">
th:remove 刪除某個屬性 <tr th:remove="all"> 1.all:刪除包含標籤和所有的孩子。
th:attr 設置標籤屬性,多個屬性可以用逗號分隔 比如 th:attr="src=@{/image/aa.jpg},title=#{logo}",此標籤不太優雅,一般用的比較少。

1、賦值、字符串拼接

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

字符串拼接還有另外一種簡潔的寫法

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

2、條件判斷 If/Unless

Thymeleaf中使用th:if和th:unless屬性進行條件判斷,下面的例子中,<a>標籤只有在th:if中條件成立時才顯示:

<a th:if="${myself=='yes'}" > </i> </a>
<a th:unless=${session.user != null} th:href="@{/login}" >Login</a>

th:unless於th:if恰好相反,只有表達式中的條件不成立,纔會顯示其內容。

也可以使用 (if) ? (then) : (else) 這種語法來判斷顯示的內容

3、for 循環

  <tr  th:each="collect,iterStat : ${collects}"> 
     <th scope="row" th:text="${collect.id}">1</th>
     <td >
        <img th:src="${collect.webLogo}"/>
     </td>
     <td th:text="${collect.url}">Mark</td>
     <td th:text="${collect.title}">Otto</td>
     <td th:text="${collect.description}">@mdo</td>
     <td th:text="${terStat.index}">index</td>
 </tr>

iterStat稱作狀態變量,屬性有:

  • index:當前迭代對象的index(從0開始計算)
  • count: 當前迭代對象的index(從1開始計算)
  • size:被迭代對象的大小
  • current:當前迭代變量
  • even/odd:布爾值,當前循環是否是偶數/奇數(從0開始計算)
  • first:布爾值,當前循環是否是第一個
  • last:布爾值,當前循環是否是最後一個

4、URL

URL在Web應用模板中佔據着十分重要的地位,需要特別注意的是Thymeleaf對於URL的處理是通過語法@{…}來處理的。 
如果需要Thymeleaf對URL進行渲染,那麼務必使用th:href,th:src等屬性,下面是一個例子

<!-- Will produce 'http://localhost:8080/standard/unread' (plus rewriting) -->
 <a  th:href="@{/standard/{type}(type=${type})}">view</a>

<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>

設置背景

<div th:style="'background:url(' + @{/<path-to-image>} + ');'"></div>

根據屬性值改變背景

 <div class="media-object resource-card-image"  th:style="'background:url(' + @{(${collect.webLogo}=='' ? 'img/favicon.png' : ${collect.webLogo})} + ')'" ></div>

幾點說明:

  • 上例中URL最後的(orderId=${o.id}) 表示將括號內的內容作爲URL參數處理,該語法避免使用字符串拼接,大大提高了可讀性
  • @{...}表達式中可以通過{orderId}訪問Context中的orderId變量
  • @{/order}是Context相關的相對路徑,在渲染時會自動添加上當前Web應用的Context名字,假設context名字爲app,那麼結果應該是/app/order

5、內聯js

內聯文本:[[…]]內聯文本的表示方式,使用時,必須先用th:inline=”text/javascript/none”激活,th:inline可以在父級標籤內使用,甚至作爲body的標籤。內聯文本儘管比th:text的代碼少,不利於原型顯示。

<script th:inline="javascript">
/*<![CDATA[*/
...
var username = /*[[${sesion.user.name}]]*/ 'Sebastian';
var size = /*[[${size}]]*/ 0;
...
/*]]>*/
</script>

js附加代碼:

/*[+
var msg = 'This is a working application';
+]*/

js移除代碼:

/*[- */
var msg = 'This is a non-working template';
/* -]*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章