thymeleaf簡介、
Thymeleaf是用於Web和獨立環境的現代服務器端Java模板引擎。
Thymeleaf的主要目標是將優雅的自然模板帶到您的開發工作流程中—HTML能夠在瀏覽器中正確顯示,並且可以作爲靜態原型,從而在開發團隊中實現更強大的協作。Thymeleaf能夠處理HTML,XML,JavaScript,CSS甚至純文本。
Thymeleaf的主要目標是提供一個優雅和高度可維護的創建模板的方式。 爲了實現這一點,它建立在自然模板的概念之上,以不影響模板作爲設計原型的方式將其邏輯注入到模板文件中。 這改善了設計溝通,彌合了前端設計和開發人員之間的理解偏差。
Thymeleaf可以處理六種模板,每種模板都稱爲模板模式。有兩種標記模板模式(HTML和XML),三種文本模板模式(TEXT,JAVASCRIPT和CSS)和一種無操作模板模式(RAW)。HTML模板模式將允許任何類型的HTML輸入,包括HTML5,HTML4和XHTML。
Spring-boot中的thymeleaf啓動器
在SpringBoot中使用thymeleaf需要依賴對應的啓動器:
<!-- thymeleaf啓動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Thymeleaf常用API介紹
1.text&value
th:text - 文本顯示屬性
th:value - 數據錄入屬性,相當於HTML標籤中的value屬性
2.字符串操作
通過Thymeleaf中的內置對象實現的。內置對象是strings。
API |
解釋 |
示例 |
isEmpty |
判斷字符串非空 |
${#strings.isEmpty(attr)} |
contains |
是否包含子串 |
${#strings.contains(attr, 'str')} |
startsWith |
是否以什麼開頭 |
${#strings.startsWith(attr, 'pre')} |
endsWith |
是否以什麼結尾 |
${#strings.endsWith(attr,'suf')} |
length |
字符串長度 |
${#strings.length(attr)} |
indexOf |
找子串索引,不存在返回-1 |
${#strings.indexOf(attr, 'str')} |
substring |
截取子串 |
${#strings.substring(attr,begin[,end])} |
toUpperCase |
轉大寫 |
${#strings.toUpperCase(attr)} |
toLowerCase |
轉小寫 |
${#strings.toLowerCase(attr)} |
3.日期操作
Thymeleaf提供內置對象,dates。作爲日期的處理工具類。
API |
解釋 |
示例 |
format |
格式化,默認Thymeleaf是使用瀏覽器的首選語言環境進行日期的格式化。 |
${#dates.format(attr)} ${#dates.format(attr, 'formatter')} |
year |
獲取年 |
${#dates.year(attr)} |
month |
獲取月份 |
${#dates.month(attr)} |
day |
獲取日期 |
${#dates.dayt(attr)} |
4.邏輯控制
判斷
<span th:if="${attr} == 'value'"> show text </span> |
分支
<div th:switch="${attr}"> <span th:case="value1">show text 1</span> <span th:case="value2">show text 2</span> <span th:case="value3">show text 3</span> </div> |
5.循環迭代
迭代線性集合
<!-- 語法: 變量名 : ${要循環的集合 attr}, 類似java中的foreach --> <tr th:each="u : ${list}"> <td th:text="${u.userid}"></td> <td th:text="${u.username}"></td> <td th:text="${u.userage}"></td> </tr> |
狀態變量,就是爲循環過程提供狀態依據的。如:循環的索引,數量,計數,奇偶數等。狀態變量在循環語法中是通用的。
<!-- 語法: 循環變量, 狀態變量 : ${attr} --> <tr th:each="u,var : ${list}"> <td th:text="${var.index}"></td><!-- 索引,從0開始 --> <td th:text="${var.count}"></td><!-- 計數,從1開始 --> <td th:text="${var.size}"></td><!-- 集合容量 --> <td th:text="${var.even}"></td><!-- 是否爲偶數 --> <td th:text="${var.odd}"></td><!-- 是否爲奇數 --> <td th:text="${var.first}"></td><!-- 是否是第一個元素 --> <td th:text="${var.last}"></td><!-- 是否是最後一個元素 --> </tr> |
迭代Map集合,在迭代Map的時候,如果使用迭代線型集合的方式迭代map,每個循環變量類型是Map.Entry。如果想操作Map.Entry中的key或value,可以進行二次迭代。在Thymeleaf中,將Map.Entry看做是一個集合。
<tr th:each="maps : ${map}"> <td th:each="entry:${maps}" th:text="${entry.key}" ></td> <td th:each="entry:${maps}" th:text="${entry.value}"></td> </tr> |
6.作用域訪問
Request:<span th:text="${req}"></span><br/> Request:<span th:text="${#httpServletRequest.getAttribute('req')}"></span><br/> Session:<span th:text="${session.sess}"></span><br/> Application:<span th:text="${application.app}"></span> |
7.URL表達式
th:action - 用於定義form表單的提交路徑。<form th:action=”@{/a}” />
th:href - 用於定義URL路徑的thymeleaf語法。
th:src - 用於定義資源路徑的thymeleaf語法。通常用於定位圖片,JS導入等。
thymeleaf中定義URL的語法爲: @{URLPath} 。
Thymeleaf會對請求頭進行encode操作。URIEncoder URIDecoder。Springmvc在處理請求頭參數的時候,會進行decode操作。
<a th:href="@{http://www.baidu.com}" th:text="絕對路徑"></a> <hr> <img th:src="@{/1.jpg}" style="height: 50px"/> <hr> <a th:href="@{/index}">相對路徑 - 相對於應用的根</a> <hr> <a th:href="@{~/index}">相對路徑 - 相對於服務器的根</a> <hr> <a th:href="@{index}">相對路徑 - 相對於當前路徑</a> <hr> <a th:href="@{/params(id=1,name=張三)}">參數傳遞</a> <hr> <a th:href="@{/restfulParams/{id}(id=2, name=李四)}">Restful傳參</a> |