1. 程式人生 > >Thymeleaf標籤整理

Thymeleaf標籤整理

1、簡單表示式

變量表達式 ${... ...}

<input type="text" name="userName" value="James Carrot" th:value="${user.name}" />

上述程式碼為引用user物件的name屬性值

選擇/星號表示式*{*** ***}

<div th:object="${session.user}">

<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>

</div>
選擇表示式一般跟在th:object後,直接取object中的屬性

文字國際化表示式 #{... ...}

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>

呼叫國際化的welcome語句,國際化資原始檔如下

resource_en_US.properties:home.welcome=Welcome to here!resource_zh_CN.properties:home.welcome=歡迎您的到來!

  URL表示式 @{... ...}

<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>

@{... ...}支援決定路徑和相對路徑。其中相對路徑又支援跨上下文呼叫url和協議的引用 當URL為後臺傳出的引數時,程式碼如下:

<img src="../../static/assets/images/qr-code.jpg" th:src="@{${path}}" alt="二維碼" />

2、常用的th標籤

簡單資料轉換(數字,日期)

<dt>價格</dt>

  <dd th:text="${#numbers.formatDecimal(product.price, 1, 2)}">180</dd>

   <dt>進貨日期</dt>

   <dd th:text="${#dates.format(product.availableFrom, 'yyyy-MM-dd')}">2014-12-01</dd>

字串拼接

<dd th:text="${'$'+product.price}">235</dd>

轉義和非轉義文字

當後臺傳出的資料為"This is an <em>HTML</em> text. <b>Enjoy yourself!</b>"時,若頁面程式碼如下則出現兩種不同的結果

<div th:text="${html}">

  This is an <em>HTML</em> text. <b>Enjoy yourself!</b>

</div> <div th:utext="${html}">

  This is an <em>HTML</em> text. <b>Enjoy yourself!</b>

</div>

表單中

<form th:action="@{/bb}" th:object="${user}" method="post" th:method="post">


<input type="text" th:field="*{name}"/>

<input type="text" th:field="*{msg}"/>


<input type="submit"/>

</form>

顯示頁面的資料迭代

//用 th:remove 移除除了第一個外的靜態資料,用第一個tr標籤進行迴圈迭代顯示    

<tbody th:remove="all-but-first">

          //將後臺傳出的 productList 的集合進行迭代,用product引數接收,通過product訪問屬性值

<tr th:each="product:${productList}">            //用count進行統計,有順序的顯示

            <td th:text="${productStat.count}">1</td>

<td th:text="${product.description}">Red Chair</td>

<td th:text="${'$' + #numbers.formatDecimal(product.price, 1, 2)}">$123</td>

<td th:text="${#dates.format(product.availableFrom, 'yyyy-MM-dd')}">2014-12-01</td>

</tr>

<tr>

<td>White table</td>

<td>$200</td>

<td>15-Jul-2013</td>

</tr>

<tr>

<td>Reb table</td>

<td>$200</td>

<td>15-Jul-2013</td>

</tr>

<tr>

<td>Blue table</td>

<td>$200</td>

<td>15-Jul-2013</td>

</tr>

</tbody>

條件判斷

<span th:if="${product.price lt 100}" class="offer">Special offer!</span>

不能用"<",">"等符號,要用"lt"等替代

<!-- 當gender存在時,選擇對應的選項;若gender不存在或為null時,取得customer物件的name-->

<td th:switch="${customer.gender?.name()}">

<img th:case="'MALE'" src="../../../images/male.png" th:src="@{/images/male.png}" alt="Male" /> <!-- Use "/images/male.png" image -->

<img th:case="'FEMALE'" src="../../../images/female.png" th:src="@{/images/female.png}" alt="Female" /> <!-- Use "/images/female.png" image -->

<span th:case="*">Unknown</span>

</td>

根據後臺資料選中select的選項

因為gender是定義的num(列舉)型別,所以要用toString方法。用th:switch指定傳出的變數,用th:case對變數的值進行匹配。!"請選擇"放在第一項會出現永遠選擇的是這個選項。或者用th:if

<div class='form-group col-lg-4'>

<select class='form-control' name="skill[4].proficiency">

<option >掌握程度</option>

<option th:if="${skill.level eq '一般'}" th:selected="selected">一般</option>

<option th:if="${skill.level eq '熟練'}" th:selected="selected">熟練</option>

<option th:if="${skill.level eq '精通'}" th:selected="selected">精通</option>

</select>

</div>

spring表示式語言

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<head>

<title>Thymeleaf tutorial: exercise 10</title>

<link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" />

<meta charset="utf-8" />

</head>

<body>

<h1>Thymeleaf tutorial - Solution for exercise 10: Spring Expression language</h1>


<h2>Arithmetic expressions</h2>

<p class="label">Four multiplied by minus six multiplied by minus two module seven:</p>

<p class="answer" th:text="${4 * -6 * -2 % 7}">123</p>


<h2>Object navigation</h2>

<p class="label">Description field of paymentMethod field of the third element of customerList bean:</p>

<p class="answer" th:text="${customerList[2].paymentMethod.description}">Credit card</p>


<h2>Object instantiation</h2>

<p class="label">Current time milliseconds:</p>

<p class="answer" th:text="${new java.util.Date().getTime()}">22-Jun-2013</p>


<h2>T operator</h2>

<p class="label">Random number:</p>

<p class="answer" th:text="${T(java.lang.Math).random()}">123456</p>

</body>

</html>

內聯

<label for="body">Message body:</label>

<textarea id="body" name="body" th:inline="text">Dear [[${customerName}]],


it is our sincere pleasure to congratulate your in your birthday:

Happy birthday [[${customerName}]]!!!See you soon, [[${customerName}]].


Regards,

The Thymeleaf team</textarea>

內聯JS<js起止加入如下程式碼,否則引號巢狀或者"<"">"等不能用>

/*<![CDATA[*/……/*]]>*/

js附加程式碼:

/*[+

var msg = 'This is a working application';

+]*/

js移除程式碼

/*[- */var msg = 'This is a non-working template';/* -]*/