日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

thymeleaf常用属性

發(fā)布時(shí)間:2025/4/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 thymeleaf常用属性 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)

作者:ITPSC 出處:http://www.cnblogs.com/hjwublog/

th:action

定義后臺(tái)控制器路徑,類似<form>標(biāo)簽的action屬性。

例如:

<form id="login-form" th:action="@{/login}">...</form>

?

th:each

對(duì)象遍歷,功能類似jstl中的<c:forEach>標(biāo)簽。

例如:

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>

?

上面的例子中通過選擇表達(dá)式*{}既能將表單綁定到后臺(tái)的StudentRequestBean中的集合屬性students,也能將Servlet上下文中的StudentRequestBean中的List類型的students變量回顯,回顯時(shí)通過th:each進(jìn)行遍歷。

注意1:綁定集合屬性元素下標(biāo)的用法*{students[__${rowStat.index}__].firstName}

注意2:如果List<Student>?students為null,頁(yè)面將無法顯示表單,后臺(tái)必須給students初始化一個(gè)值,即:

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>標(biāo)簽的href?屬性。value形式為@{/logout}

例如:

<a th:href="@{/logout}" class="signOut"></a>

?

th:id

div?id聲明,類似html標(biāo)簽中的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為模板片段,常用與頭文件、頁(yè)尾文件的引入。常與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

用于表單數(shù)據(jù)對(duì)象綁定,將表單綁定到后臺(tái)controller的一個(gè)JavaBean參數(shù)。常與th:field一起使用進(jìn)行表單數(shù)據(jù)綁定。

例如:

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>標(biāo)簽的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

用于標(biāo)簽復(fù)制,類似<option>標(biāo)簽的value屬性。

例如:

<option th:value="Adult">Adult</option><input id="msg" type="hidden" th:value="${msg}" />


轉(zhuǎn)載于:https://www.cnblogs.com/honger/p/6876046.html

總結(jié)

以上是生活随笔為你收集整理的thymeleaf常用属性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。