Thymeleaf 模板引擎用法
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}" />
作者:ITPSC
出处:http://www.cnblogs.com/hjwublog/
温馨提示:当您看到这篇文章时,我可能在很久之前就已经准备了,如果您觉得阅读本文能让你有所收获,请点一下“推荐”按钮或者“关注我”按钮,您的肯定将是我写作的动力!欢迎转载,转载请注明出处!
搜索
最新评论
- 1. Re:mybatis基础系列(四)——关联查询、延迟加载、一级缓存与二级缓存
- @等待是一生最初的苍老@等待是一生最初的苍老引用@ITPSC引用引用@等待是一生最初的苍老 mybatis竟然提供了缓存的功能,肯定是有用的。如果你没用第三方缓存技术(如redis),我认为它就有必要......
- --ITPSC
- 2. Re:mybatis基础系列(四)——关联查询、延迟加载、一级缓存与二级缓存
- @ITPSC引用@等待是一生最初的苍老 mybatis竟然提供了缓存的功能,肯定是有用的。如果你没用第三方缓存技术(如redis),我认为它就有必要。MyBatis 利用本地缓存机制(Local Ca......
- --等待是一生最初的苍老
- 3. Re:mybatis基础系列(四)——关联查询、延迟加载、一级缓存与二级缓存
- @等待是一生最初的苍老 mybatis竟然提供了缓存的功能,肯定是有用的。如果你没用第三方缓存技术(如redis),我认为它就有必要。MyBatis 利用本地缓存机制(Local Cache),也就是......
- --ITPSC
- 4. Re:mybatis基础系列(四)——关联查询、延迟加载、一级缓存与二级缓存
- 楼主觉得他的缓存有必要吗? 一级缓存有没有全局禁用的方法
- --等待是一生最初的苍老
- 5. Re:Thymeleaf 常用属性
- @昨日的世界感谢支持...
- --ITPSC
- 1. th:action
- 2. th:each
- 3. th:field
- 4. th:href
- 5. th:id
- 6. th:if
- 7. th:include
- 8. th:fragment
- 9. th:object
- 10. th:src
- 11. th:replace
- 12. th:text
- 13. th:value
Thymeleaf 模板引擎用法的更多相关文章
- thymeleaf模板引擎
thymeleaf模板引擎 thymeleaf是现代化服务器端的Java模板引擎,不同于JSP和FreeMarker,Thymeleaf的语法更加接近HTML,并且也有不错的扩展性.详细资料可以浏览官 ...
- (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用
一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- 三、thymeleaf模板引擎构建前台html, 后台使用 ModelAndView 和 Model 模型
项目源码:https://github.com/y369q369/springBoot.git -> thymeleaf 私聊QQ: 1486866853 1.pom.xml中 ...
- SpringBoot使用thymeleaf模板引擎
(1).添加pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- Spring Boot 2.0 整合Thymeleaf 模板引擎
本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...
- Thymeleaf模板引擎的初步使用
在springboot中,推荐使用的模板引擎是Thymeleaf模板引擎,它提供了完美的Spring MVC的支持.下面就简单的介绍一下Thymeleaf模板引擎的使用. 在controller层中, ...
- spring boot: thymeleaf模板引擎使用
spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依赖 <!-- 添加thymeleaf的依赖 --> <dependency> ...
- SpringBoot入门篇--使用Thymeleaf模板引擎进行页面的渲染
在做WEB开发的时候,我们不可避免的就是在前端页面之间进行跳转,中间进行数据的查询等等操作.我们在使用SpringBoot之前包括我在内其实大部分都是用的是JSP页面,可以说使用的已经很熟悉.但是我们 ...
随机推荐
- python 浮点运算 及 小数点精确位数
>>> 1050 / 3133>>> 1050 / float(31)33.87096774193548 # 分子或者分母用 ...
- Nginx 作为 WebSockets 代理
WebSocket 协议给我们提供了一个创建可以支持客户端和服务端进行双向实时通信的web应用程序的方法.相比之前使用的方法,WebSocket(作为HTML5的一部分)可以使我们更容易开的发出这种类 ...
- MaBatis(5)输入/输出映射
本次全部学习内容:MyBatisLearning 输入映射: 通过parameType指定输入参数的类型,类型可以是简单类型,hashmap,pojo等 传递pojo的包装对象 需求: 即 ...
- 一. Selenium介绍
1. 什么是Selenium 是web自动化测试工具集,主要包括:IDE.Grid.RC(Selenium1.0).WebDriver(Selenium2.0) 与其他工具的不同: 一般的脚本测试工具 ...
- javascript-数字转罗马数字
阿拉伯数字与罗马数字转换 罗马数字表示 XXI, 21 个位数举例I, 1 ]II, 2] III, 3] IV, 4 ]V, 5 ]VI, 6] VII, 7] VIII,8 ]IX, 9 ·十位数 ...
- OC和C语言比较
说明:比较记忆相对来说更容易熟练记得牢固,理解了C语言相对来说OC也不太难,OC是C语言的扩展,向下兼容C语言. 源文件后缀名比较 1.C语言源文件 .h:头文件 .c:源文件 .o:目标文件 .ou ...
- .net MVC 页面页面跳转后提示消息实现办法
mvc在RedirectToAction之后,会清理掉ViewData中的所有数据,因此通过ViewData给下一个页面传递提示消息不太好,如果是通过参数方式传递,刷新跳转后的页面时,消息还会再次提示 ...
- 10.vue router 带参数跳转
vue router 带参数跳转 发送:this.$router.push({path:'/news',query:{id:row.id}}) 接收:var id=this.$route.query. ...
- ABAP术语-Sales Document
Sales Document 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/13/1103294.html Data base docume ...
- .net core 基于Claim登录验证
网站,首先需要安全,实现安全就必须使用登录验证,.net core 基于Claim登录验证就很简单使用. Claim是什么,可以理解为你的身份证的中的名字,性别等等的每一条信息,然后Claim组成一个 ...



