thymeleaf常用属性
轉
作者:ITPSC 出處:http://www.cnblogs.com/hjwublog/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}" />轉載于:https://www.cnblogs.com/honger/p/6876046.html
總結
以上是生活随笔為你收集整理的thymeleaf常用属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 五大经常使用算法 之 动态规划法
- 下一篇: 《构建之法》读后感5