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}" />
Thymeleaf 常用属性的更多相关文章
- thymeleaf常用属性
转 作者:ITPSC 出处:http://www.cnblogs.com/hjwublog/ th:action 定义后台控制器路径,类似<form>标签的action属性. 例如: ...
- 【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.当您把鼠标指针移动到网页中的某个链接上时,箭头会 ...
随机推荐
- ios企业应用发布流程
转载自:http://blog.csdn.net/justinjing0612/article/details/8758692留作备忘 企业发布app的 过程比app store 发布的简单多了,没那 ...
- HTML简历表格
效果图 <!DOCTYPE > <html> <head> <meta charset="utf-8" /> </head&g ...
- 20145206邹京儒《Java程序设计》第7周学习总结
20145206 <Java程序设计>第7周学习总结 教材学习内容总结 第十三章 时间与日期 13.1.1 时间的度量 ·即使标注为GMT(格林威治时间),实际上谈到的的是UTC(Unix ...
- Yslow网站性能优化工具
Yslow是一款网站性能优化的插件:
- tomcat7源码编译过程以及问题解决
http://blog.csdn.net/kaoshangqinghua/article/details/40022315
- Java Eclipse进行断点调试
如何调试Java程序? 大家最开始学习Java,都会觉得IDE调试好高端有木有,其实很简单了. 下文会尽量简单直观的教会你在Eclipse中调试,其他的IDE调试步骤也是类似的. 1.在你觉得有错的地 ...
- JAVA基础学习之流的简述及演示案例、用缓冲区方法buffer读写文件、File类对象的使用、Serializable标记接口(6)
1.流的简述及演示案例输入流和输出流相对于内存设备而言.将外设中的数据读取到内存中:输入将内存的数写入到外设中:输出.字符流的由来:其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表. ...
- python中的Iterable, Iterator,生成器概念
https://nychent.github.io/articles/2016-05/about-generator.cn 这个深刻 谈起Generator, 与之相关的的概念有 - {list, s ...
- .Learning.Python.Design.Patterns.2nd.Edition之单实例模式
可以慢慢理解.. 对照JAVA class Singleton(object): def __new__(cls): if not hasattr(cls, 'instance'): cls.inst ...
- <转>FreeMarker内置函数
一. Sequence的内置函数1. sequence?first 返回sequence的第一个值.2. sequence?last 返回sequence的最后一个值.3. sequence?reve ...