thymeleaf常用属性
转
- 作者:ITPSC
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.当您把鼠标指针移动到网页中的某个链接上时,箭头会 ...
随机推荐
- 源码解读·RT-Thread多任务调度算法
*本文依据RT-Thread当时最新版本4.0.1版本源码 RT-Thread操作系统是一款基于优先级和时间片轮转的多任务实时操作系统.其调度算法采用256个优先级,并支持相同优先级的任务存在.不同优 ...
- 错误处理之try、catch、finally中的return、throw执行顺序。
今天遇到一个让人无语的代码块 try { bilSheetService.syncUser(bilWebseviceLog, userId, optType); }catch (Exception e ...
- Hive 学习之路(三)—— Hive CLI和Beeline命令行的基本使用
一.Hive CLI 1.1 Help 使用hive -H或者 hive --help命令可以查看所有命令的帮助,显示如下: usage: hive -d,--define <key=value ...
- Python生成word
Python生成word 使用python-docx-template库, 将html转为word. python-docx-template可以使用类似jinja2的模板语法. 依赖docx库, 安 ...
- zabbix报警信息设置
报警信息 默认标题: 服务器监控报警 告警主机:{HOST.NAME} 主机IP: {HOST.IP} 告警级别:{TRIGGER.SEVERITY} 告警信息:{TRIGGER.NAME} 问题详情 ...
- 01-Javascript基础
一. JS介绍 JavaScript是前台语言 JavaScript是前台语言,而不是后台语言. JavaScript运行在用户的终端网页上,而不是服务器上,所以我们称为“前台语言”. JavaScr ...
- Web自动化测试 一
Web自动化测试 一.为什么要进行web自动化测试 接口测试只能测试后端返回的数据,定位的是后端开发工程师的问题.如果前段出现了问题,我们要使用web测试去发现错误. 具体定位的问题有: 显示的数据: ...
- [Vue 牛刀小试]:第十五章 - 传统开发模式下的 axios 使用入门
一.前言 在没有接触 React.Angular.Vue 这类 MVVM 的前端框架之前,无法抛弃 Jquery 的重要理由,除了优秀的前端 DOM 元素操作性以外,能够非常便捷的发起 http 请求 ...
- python工具函数
1. translate translate要比replace要高效,translate支持替换多 使用translate之前必须要创建一个转换表.要创建转换表,可对字符串类型str调用方法maket ...
- css实现超出文本溢出用省略号代替
一.单行 实现单行文本的溢出显示省略号使用text-overflow:ellipsis属性,但需要配合使用另外两个属性使用才能达到效果. 如下: overflow:hidden; text-overf ...