官方網站:http://www.thymeleaf.org/html
thymeleaf簡介git
Thymeleaf 是一個跟 Velocity、FreeMarker 相似的模板引擎,它能夠徹底替代 JSP 。相較與其餘的模板引擎,它有以下三個極吸引人的特色:程序員
1.Thymeleaf 在有網絡和無網絡的環境下皆可運行,即它能夠讓美工在瀏覽器查看頁面的靜態效果,也能夠讓程序員在服務器查看帶數據的動態頁面效果。這是因爲它支持 html 原型,而後在 html 標籤裏增長額外的屬性來達到模板+數據的展現方式。瀏覽器解釋 html 時會忽略未定義的標籤屬性,因此 thymeleaf 的模板能夠靜態地運行;當有數據返回到頁面時,Thymeleaf 標籤會動態地替換掉靜態內容,使頁面動態顯示。github
2.Thymeleaf 開箱即用的特性。它提供標準和spring標準兩種方言,能夠直接套用模板實現JSTL、 OGNL表達式效果,避免天天套模板、改jstl、改標籤的困擾。同時開發人員也能夠擴展和建立自定義的方言。web
3. Thymeleaf 提供spring標準方言和一個與 SpringMVC 完美集成的可選模塊,能夠快速的實現表單綁定、屬性編輯器、國際化等功能。spring
表達式瀏覽器
變量表達式"${ }" 服務器
<span th:text="${book.author.name}">
消息表達式"#{ }" 網絡
<span th:text="#{book.author.name}">
(文本外部化、國際化、i18n)app
選擇表達式"*{ }"
<div th:text="${book}"> <span th:text="*{name}"></span> </div>
連接表達式 "@{ }"
<a th:href="@{../doucments/report}">...</a> <a th:href="@{~/doucments/report}">...</a> <a th:href="@{//static.mycompany.com/report}">...</a> <a th:href="@{http://www.mycompany.com/doucments/report}">...</a>
分段表達式 th:insert th:replace
<!doctype html> <html xmls:th="http://www.thymeleaf.org"> <body> <div th:fragment="copy"> © 2017 <a href="https//waylau.com">waylau.com</a> </body> </html>
<body> ... <div th:insert="~{foot :: copy }"></div> </body>
字面量
文本
<p> Now you are looking at a <span th:text="'working web application'">template file</span>. </p>
數字
<p> The year is <span th:text="2013">1492</span>. </p>
布爾 true、false
空 null
算術操做
+、-、*、/、%
比較:>、<、>=、<=(gt、lt、ge、le)
等價 :==、!= (eq ne)
三目運算符 ? :
無操做 _
字面量(文字)
null
<div th:if="${variable.something} ==null"></div>
參考資料:https://github.com/waylau/thymeleaf-tutorial