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}" />
thymeleaf常用属性的更多相关文章
- Thymeleaf 常用属性
Thymeleaf 常用属性 如需了解thymeleafThymeleaf 基本表达式,请参考<Thymeleaf 基本表达式>一文 th:action 定义后台控制器路径,类似<f ...
- 【Thymeleaf】常用属性
参考链接 Thymeleaf 常用属性
- Thymeleaf常用语法:HTML属性设置
使用Thymeleaf的属性来设置HTML属性.(1)使用th:attr属性可以修改原来HTML节点的属性:(2)th:attr属性可以同时设置多个属性:(3)每一个HTML属性都有对应的Thymel ...
- 【转】Spring Boot干货系列:常用属性汇总
转自Spring Boot干货系列:常用属性汇总 附录A.常用应用程序属性 摘自:http://docs.spring.io/spring-boot/docs/current/reference/ht ...
- 【Android自学日记】五大布局常用属性
线性布局(LinearLayout)常用属性: android:orientation="vertical"--决定子类控件的排布方式(vertical垂直:horizontal水 ...
- DataGrid中的常用属性
DataGrid中的常用属性 $('#dg').datagrid({ url:'datagrid_data.json', columns:[[ {field:'code',title:'Code',w ...
- Node.js process 模块常用属性和方法
Node.js是常用的Javascript运行环境,本文和大家发分享的主要是Node.js中process 模块的常用属性和方法,希望通过本文的分享,对大家学习Node.js http://www.m ...
- ImageView的常用属性
ImageView的一些常用属性,并且这些属性都有与之对应的getter.setter方法: android:adjustViewBounds:设置ImageView是否调整自己的边界来保持所显示图片 ...
- HTML a标签、4个伪类、常用属性(下载)、锚链接(待扩展:邮件、电话、短信、GPS)
HTML 超链接<a> 1.超链接可以是一个字.一个词.一组词.一幅图像,您可以点击这些内容来跳转到新的文档或者当前文档中的某个部分. 2.当您把鼠标指针移动到网页中的某个链接上时,箭头会 ...
随机推荐
- Servlet 3.0异步特性初探
Servlet 是 Java 为了编写服务端程序而定义的一个接口规范,在 Servlet 3.0 以后支持了异步的操作. 最近项目添加了一个代码热部署的功能,在客户端输入信号,信号到达 Web 服务器 ...
- Fabric1.4源码解析: 链码容器启动过程
想写点东西记录一下最近看的一些Fabric源码,本文使用的是fabric1.4的版本,所以对于其他版本的fabric,内容可能会有所不同. 本文想针对Fabric中链码容器的启动过程进行源码的解析.这 ...
- HBase —— 单机环境搭建
一.安装前置条件说明 1.1 JDK版本说明 HBase 需要依赖JDK环境,同时HBase 2.0+ 以上版本不再支持JDK 1.7 ,需要安装JDK 1.8+ .JDK 安装方式见本仓库: Lin ...
- Apicloud 接入海康摄像头
1准备工作 , 加载apicloud 海康视频模块. 引入 SDK 重新生成项目测试 再config.xml写入appid 话不多说直接上代码 video=api.require("haik ...
- 【Go】使用压缩文件优化io (二)
原文链接: https://blog.thinkeridea.com/201907/go/compress_file_io_optimization2.html 上一篇文章<使用压缩文件优化io ...
- 关于char[]转换成LPCWSTR的有关问题[转]
一.问题的原因:VS2010默认采用宽字符UNICODE编码方式,定义了Unicode,因此相关的字符串必须为unicode字符串,而非ascii字符串. LPCWSTR中的W是宽字符的意思,是UNI ...
- HDU 6181:Two Paths(A* + SPFA)
题目链接 题意 给出n个点m条边的无向图,求次短路. 思路 和 POJ 2449 类似,只不过大小要开成long long. #include <bits/stdc++.h> using ...
- BZOJ 1878:[SDOI2009]HH的项链(莫队算法)
http://www.lydsy.com/JudgeOnline/problem.php?id=1878 题意:…… 思路:比上题还简单很多.数字很小,开一个数组哈希记录出现次数(记得数组要开1e6) ...
- mongo创建集合
查询 db.getCollection('tbTrade').find({strDealSN:'P2P0_153596710bb00010011ba47b342'}); 更新(第一个{}为条件) db ...
- mysql计算日期之间相差的天数
TO_DAYS(NOW()) - TO_DAYS(createTime) as dayFactor,