Thymeleaf 簡介
Thymeleaf (https://www.thymeleaf.org/ Thymeleaf 3.0.15) 是一個XML/XHTML/HTML5模板引擎,可用於Web與非Web環境中的應用開發。它是一個開源的Java庫,基於Apache License 2.0許可。
Thymeleaf提供了一個用於整合Spring MVC的可選模塊,在應用開發中,你可以使用Thymeleaf來完全代替JSP,或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目標在於提供一種可被瀏覽器正確顯示的、格式良好的模板創建方式,因此也可以用作靜態建模。你可以使用它創建經過驗證的XML與HTML模板。相對於編寫邏輯或代碼,開發者只需將標籤屬性添加到模板中即可。接下來,這些標籤屬性就會在DOM(文檔對象模型)上執行預先制定好的邏輯。
Thymeleaf 是一款用於渲染 XML/XHTML/HTML5 內容的模板引擎。它與 JSP,Velocity,FreeMaker 等模板引擎類似,也可以輕易地與 Spring MVC 等 Web 框架集成。與其它模板引擎相比,Thymeleaf 最大的特點是,即使不啓動 Web 應用,也可以直接在瀏覽器中打開並正確顯示模板頁面 。
Thymeleaf 是新一代 Java 模板引擎,支持 HTML 原型,以直接被瀏覽器打開,此時瀏覽器會忽略未定義的 Thymeleaf 標籤屬性,展示 thymeleaf 模板的靜態頁面效果。當在應用程序中會動態地替換掉頁面設置的標籤屬性。
示例模板:
<table>
<thead>
<tr>
<th th:text="#{msgs.headers.name}">Name</td>
<th th:text="#{msgs.headers.price}">Price</td>
</tr>
</thead>
<tbody>
<tr th:each="prod : ${allProducts}">
<td th:text="${prod.name}">Oranges</td>
<td th:text="${#numbers.formatDecimal(prod.price,1,2)}">0.99</td>
</tr>
</tbody>
</table>
Thymeleaf 的特點
-
動靜結合:Thymeleaf 既可以直接使用瀏覽器打開,查看頁面的靜態效果,也可以通過 Web 應用程序進行訪問,查看動態頁面效果。
-
開箱即用:Thymeleaf 提供了 Spring 標準方言以及一個與 Spring MVC 完美集成的可選模塊,可以快速地實現表單綁定、屬性編輯器、國際化等功能。
-
多方言支持:它提供了 Thymeleaf 標準和 Spring 標準兩種方言,可以直接套用模板實現 JSTL、 OGNL 表達式;必要時,開發人員也可以擴展和創建自定義的方言。
-
與 Spring Boot 完美整合:Spring Boot 爲 Thymeleaf 提供了的默認配置,並且還爲 Thymeleaf 設置了視圖解析器,因此 Thymeleaf 可以與 Spring Boot 完美整合。
Thymeleaf常用標籤
Thymeleaf標準表達式
變量表達式 ${}
使用方法:直接使用th:xx = "${}"
獲取對象屬性 。例如:
<form id="userForm"> <input id="id" name="id" th:value="${user.id}"/> <input id="username" name="username" th:value="${user.username}"/> <input id="password" name="password" th:value="${user.password}"/> </form> <div th:text="hello"></div> <div th:text="${user.username}"></div>
選擇變量表達式 *{}
使用方法:首先通過th:object
獲取對象,然後使用th:xx = "*{}"
獲取對象屬性。
這種簡寫風格極爲清爽,推薦大家在實際項目中使用。 例如:
<form id="userForm" th:object="${user}"> <input id="id" name="id" th:value="*{id}"/> <input id="username" name="username" th:value="*{username}"/> <input id="password" name="password" th:value="*{password}"/> </form>
URL表達式 @{}
使用方法:通過鏈接表達式@{}
直接拿到應用路徑,然後拼接靜態資源路徑。例如:
<script th:src="@{/webjars/jquery/jquery.js}"></script> <link th:href="@{/webjars/bootstrap/css/bootstrap.css}" rel="stylesheet" type="text/css">
片段表達式 ~{}
片段表達式是Thymeleaf的特色之一,細粒度可以達到標籤級別,這是JSP無法做到的。
片段表達式擁有三種語法:
~{ viewName } 表示引入完整頁面
~{ viewName ::selector} 表示在指定頁面尋找片段 其中selector可爲片段名、jquery選擇器等
~{ ::selector} 表示在當前頁尋找
使用方法:首先通過th:fragment
定製片段 ,然後通過th:replace
填寫片段路徑和片段名。例如:
<!-- /views/common/head.html--> <head th:fragment="static"> <script th:src="@{/webjars/jquery/3.3.1/jquery.js}"></script> </head> <!-- /views/your.html --> <div th:replace="~{common/head::static}"></div>
在實際使用中,我們往往使用更簡潔的表達,去掉表達式外殼直接填寫片段名。例如:
<!-- your.html --> <div th:replace="common/head::static"></div>
注意:使用替換路徑th:replace
開頭請勿添加斜槓,避免部署運行的時候出現路徑報錯。(因爲默認拼接的路徑爲spring.thymeleaf.prefix = classpath:/templates/
)
消息表達式
即通常的國際化屬性:#{msg}
用於獲取國際化語言翻譯值。例如:
<title th:text="#{user.title}"></title>
其它表達式
在基礎語法中,默認支持字符串連接、數學運算、布爾邏輯和三目運算等。例如:
<input name="name" th:value="${'I am '+(user.name!=null?user.name:'NoBody')}"/>
Thymeleaf模板基本配置
Thymeleaf靜態資源的訪問
https://baijiahao.baidu.com/s?id=1715241442804420499&wfr=spider&for=pc
https://www.cnblogs.com/youngdeng/p/12858055.html