如需瞭解Thymeleaf 基本表達式,請參考《Thymeleaf 基本表達式》一文html
定義後臺控制器路徑,相似<form>標籤的action屬性。jquery
例如:json
<form id="login-form" th:action="@{/login}">...</form>
對象遍歷,功能相似jstl中的<c:forEach>標籤。app
例如:spa
public class StudentRequestBean { private List<Student> students; ... } public class Student implements Serializable{ private String firstName; private String school; ...}
@RequestMapping(value = "/addStudent", method = RequestMethod.POST) public String addStudent(@ModelAttribute(value = "stuReqBean") StudentRequestBean stuReqBean,ModelMap model) {...}
<form id="login-form" th:action="@{/addStudent}" th:object="${stuReqBean}" method="POST"> <div class="student" th:each="stuIter,rowStat:${stuReqBean.students}"> <input type="text" class="firstName" value="" th:field="*{students[__${rowStat.index}__].firstName}"></input> <input type="text" class="school" value="" th:field="*{students[__${rowStat.index}__].school}"></input> ... </div> </form>
上面的例子中經過選擇表達式*{}既能將表單綁定到後臺的StudentRequestBean中的集合屬性students,也能將Servlet上下文中的StudentRequestBean中的List類型的students變量回顯,回顯時經過th:each進行遍歷。code
注意1:綁定集合屬性元素下標的用法*{students[__${rowStat.index}__].firstName}orm
注意2:若是List<Student> students爲null,頁面將沒法顯示錶單,後臺必須給students初始化一個值,即:htm
List<Student > stus = new ArrayList<Student >(); stus .add(new Student ()); StudentRequestBean.setStudents(stus );
注意3:stuIter表明students的迭代器對象
經常使用於表單字段綁定。一般與th:object一塊兒使用。 屬性綁定、集合綁定。blog
如:
public class LoginBean implements Serializable{... private String username; private List<User> user; ...} public class User implements Serializable{... private String username;; ...} @RequestMapping(value = "/login", method = RequestMethod.POST) public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model) {..}
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">... <input type="text" value="" th:field="*{username}"></input> <input type="text" value="" th:field="*{user[0].username}"></input> </form>
定義超連接,相似<a>標籤的href 屬性。value形式爲@{/logout}
例如:
<a th:href="@{/logout}" class="signOut"></a>
div id聲明,相似html標籤中的id屬性。
例如:
<div class="student" th:id = "stu+(${rowStat.index}+1)"></div>
條件判斷。
例如:
<div th:if="${rowStat.index} == 0">... do something ...</div>
見th:fragment
聲明定義該屬性的div爲模板片斷,經常使用與頭文件、頁尾文件的引入。常與th:include,th:replace一塊兒使用。
例如:
聲明模板片斷/WEBINF/templates/footer. html
<div th: fragment=" copy" > © 2011 The Good Thymes Virtual Grocery </div>
引入模板片斷
<div th: include=" /templates/footer : : copy" ></div> <div th: replace=" /templates/footer : : copy" ></div>
用於表單數據對象綁定,將表單綁定到後臺controller的一個JavaBean參數。常與th:field一塊兒使用進行表單數據綁定。
例如:
public class LoginBean implements Serializable{...}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model) {...}
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...</form>
用於外部資源引入,相似於<script>標籤的src屬性,常與@{}一塊兒使用。
例如:
<script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"
見th:fragment
文本顯示。
例如:
<td class="text" th:text="${username}" ></td>
用於標籤複製,相似<option>標籤的value屬性。
例如:
<option th:value="Adult">Adult</option> <input id="msg" type="hidden" th:value="${msg}" />