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页面,可以说使用的已经很熟悉.但是我们 ...
随机推荐
- jsp 页面显示格式化的日期
在页面引入 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 使用 ...
- POJ 1845 Sumdiv 【二分 || 逆元】
任意门:http://poj.org/problem?id=1845. Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions ...
- 14、SpringBoot-CRUD错误处理机制(1)
一.springboot默认的处理机制 1.浏览器返回一个错误的页面 默认处理错误:返回一个错误的页面: 包括错误类型.时间...... 2.其他客户端访问 默认响应一个json数据 原理: 错误 ...
- REG小探
根键名称缩写对照表 常用数据类型
- 火狐中jq的attr出现的bug问题用prop代替
再工作的时候遇到一个很奇怪的问题 ,就是attr属性不好使!就问度娘去了...... 结果如下: .prop() 1..prop( propertyName ) 获取匹配集合中第一个元素的Prop ...
- HUD 1288 Hat's Tea(反向的贪心,非常好的一道题)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1288 Hat's Tea Time Limit: 2000/1000 MS (Java/Others) ...
- HTML5——前端预处理技术(Less、Sass、CoffeeScript)
一.Less 1.1.概要 Less是一种动态样式语言,Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展. Less 将 C ...
- Reading Notes : 180211 概述计算机
读书<计算机组成原理> <鸟哥的Linux私房菜 基础篇> 本章介绍电子计算机概念以及发展历史和发展趋势,内容摘自<计算机组成原理> <鸟哥的Linux私房 ...
- 更新UI放在主线程的原因
1.在子线程中是不能进行UI 更新的,而可以立刻更新的原因是:子线程代码执行完毕了,又自动进入到了主线程,这中间的时间非常的短,让我们误以为子线程可以更新UI.如果子线程一直在运行,则无法更新UI,因 ...
- java中实现多线程的几种方式(简单实现)
一.以下只是简单的实现多线程 1:继承Thread 2:实现 Runnable 3:实现callable 如果需要返回值使用callable,如果不需要返回最好使用runnable,因为继承只能单继承 ...



