Thymeleaf 常用屬性
如需了解Thymeleaf 基本表達式,請參考《Thymeleaf 基本表達式》一文
th:action
定義后台控制器路徑,類似<form>標簽的action屬性。
例如:
<form id="login-form" th:action="@{/login}">...</form>
th:each
對象遍歷,功能類似jstl中的<c:forEach>標簽。
例如:
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進行遍歷。
注意1:綁定集合屬性元素下標的用法*{students[__${rowStat.index}__].firstName}
注意2:如果List<Student> students為null,頁面將無法顯示表單,后台必須給students初始化一個值,即:
List<Student > stus = new ArrayList<Student >(); stus .add(new Student ()); StudentRequestBean.setStudents(stus );
注意3:stuIter代表students的迭代器
th:field
常用於表單字段綁定。通常與th:object一起使用。 屬性綁定、集合綁定。
如:
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>
th:href
定義超鏈接,類似<a>標簽的href 屬性。value形式為@{/logout}
例如:
<a th:href="@{/logout}" class="signOut"></a>
th:id
div id聲明,類似html標簽中的id屬性。
例如:
<div class="student" th:id = "stu+(${rowStat.index}+1)"></div>
th:if
條件判斷。
例如:
<div th:if="${rowStat.index} == 0">... do something ...</div>
th:include
見th:fragment
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>
th:object
用於表單數據對象綁定,將表單綁定到后台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>
th:src
用於外部資源引入,類似於<script>標簽的src屬性,常與@{}一起使用。
例如:
<script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"
th:replace
見th:fragment
th:text
文本顯示。
例如:
<td class="text" th:text="${username}" ></td>
th:value
用於標簽復制,類似<option>標簽的value屬性。
例如:
<option th:value="Adult">Adult</option> <input id="msg" type="hidden" th:value="${msg}" />
作者:ITPSC
出處:http://www.cnblogs.com/hjwublog/
溫馨提示:當您看到這篇文章時,我可能在很久之前就已經准備了,如果您覺得閱讀本文能讓你有所收獲,請點一下“推薦”按鈕或者“關注我”按鈕,您的肯定將是我寫作的動力!歡迎轉載,轉載請注明出處!