随笔- 31  文章- 0  评论- 50 

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/
温馨提示:当您看到这篇文章时,我可能在很久之前就已经准备了,如果您觉得阅读本文能让你有所收获,请点一下“推荐”按钮或者“关注我”按钮,您的肯定将是我写作的动力!欢迎转载,转载请注明出处

 
分类: thymeleaf
 
好文要顶 关注我 收藏该文  
9
3
 
 
 
posted @ 2015-12-16 17:18 ITPSC 阅读(85530) 评论(7) 编辑 收藏
 
评论
 

#1楼 2018-10-23 09:56 | 测试5454 

我不知道你写这些意义在哪里?

#2楼[楼主] 2018-10-23 10:06 | ITPSC 

@ 天德社区
这位老师有何看法,交流交流?

#3楼 2018-10-24 16:17 | 测试5454 

@ ITPSC
不,我只是感觉你写的太简洁,会有很多人看不懂

#4楼 2018-10-24 16:19 | 测试5454 

@ ITPSC
引用@天德社区
这位老师有何看法,交流交流?
并没有其他的意思

#5楼[楼主] 2018-10-24 17:20 | ITPSC 

@ 天德社区
引用@ITPSC
不,我只是感觉你写的太简洁,会有很多人看不懂
结合前面几篇文章,顺着看应该能看懂吧,这篇常用属性了解html的都应该能看懂。

#6楼 2018-11-13 15:33 | 昨日的世界 

我正好要用,看到这篇,当作入门手册,很不错!

#7楼[楼主] 2018-11-13 15:42 | ITPSC 

@ 昨日的世界
感谢支持
 
 
发表评论

昵称:

评论内容:
     
 

退出 订阅评论

 

[Ctrl+Enter快捷键提交]

 
 
 
昵称:ITPSC
园龄:3年5个月
粉丝:128
关注:7

< 2018年12月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

搜索

 
 
 

最新评论

 
 
Copyright ©2018 ITPSC
 
 

Thymeleaf 模板引擎用法的更多相关文章

  1. thymeleaf模板引擎

    thymeleaf模板引擎 thymeleaf是现代化服务器端的Java模板引擎,不同于JSP和FreeMarker,Thymeleaf的语法更加接近HTML,并且也有不错的扩展性.详细资料可以浏览官 ...

  2. (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

    一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...

  3. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  4. 三、thymeleaf模板引擎构建前台html, 后台使用 ModelAndView 和 Model 模型

    项目源码:https://github.com/y369q369/springBoot.git      ->     thymeleaf 私聊QQ: 1486866853 1.pom.xml中 ...

  5. SpringBoot使用thymeleaf模板引擎

    (1).添加pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  6. Spring Boot 2.0 整合Thymeleaf 模板引擎

    本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...

  7. Thymeleaf模板引擎的初步使用

    在springboot中,推荐使用的模板引擎是Thymeleaf模板引擎,它提供了完美的Spring MVC的支持.下面就简单的介绍一下Thymeleaf模板引擎的使用. 在controller层中, ...

  8. spring boot: thymeleaf模板引擎使用

    spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依赖 <!-- 添加thymeleaf的依赖 --> <dependency> ...

  9. SpringBoot入门篇--使用Thymeleaf模板引擎进行页面的渲染

    在做WEB开发的时候,我们不可避免的就是在前端页面之间进行跳转,中间进行数据的查询等等操作.我们在使用SpringBoot之前包括我在内其实大部分都是用的是JSP页面,可以说使用的已经很熟悉.但是我们 ...

随机推荐

  1. python 浮点运算 及 小数点精确位数

    >>> 1050 / 3133>>> 1050 / float(31)33.87096774193548                     # 分子或者分母用 ...

  2. Nginx 作为 WebSockets 代理

    WebSocket 协议给我们提供了一个创建可以支持客户端和服务端进行双向实时通信的web应用程序的方法.相比之前使用的方法,WebSocket(作为HTML5的一部分)可以使我们更容易开的发出这种类 ...

  3. MaBatis(5)输入/输出映射

    本次全部学习内容:MyBatisLearning   输入映射: 通过parameType指定输入参数的类型,类型可以是简单类型,hashmap,pojo等     传递pojo的包装对象 需求: 即 ...

  4. 一. Selenium介绍

    1. 什么是Selenium 是web自动化测试工具集,主要包括:IDE.Grid.RC(Selenium1.0).WebDriver(Selenium2.0) 与其他工具的不同: 一般的脚本测试工具 ...

  5. javascript-数字转罗马数字

    阿拉伯数字与罗马数字转换 罗马数字表示 XXI, 21 个位数举例I, 1 ]II, 2] III, 3] IV, 4 ]V, 5 ]VI, 6] VII, 7] VIII,8 ]IX, 9 ·十位数 ...

  6. OC和C语言比较

    说明:比较记忆相对来说更容易熟练记得牢固,理解了C语言相对来说OC也不太难,OC是C语言的扩展,向下兼容C语言. 源文件后缀名比较 1.C语言源文件 .h:头文件 .c:源文件 .o:目标文件 .ou ...

  7. .net MVC 页面页面跳转后提示消息实现办法

    mvc在RedirectToAction之后,会清理掉ViewData中的所有数据,因此通过ViewData给下一个页面传递提示消息不太好,如果是通过参数方式传递,刷新跳转后的页面时,消息还会再次提示 ...

  8. 10.vue router 带参数跳转

    vue router 带参数跳转 发送:this.$router.push({path:'/news',query:{id:row.id}}) 接收:var id=this.$route.query. ...

  9. ABAP术语-Sales Document

    Sales Document 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/13/1103294.html Data base docume ...

  10. .net core 基于Claim登录验证

    网站,首先需要安全,实现安全就必须使用登录验证,.net core 基于Claim登录验证就很简单使用. Claim是什么,可以理解为你的身份证的中的名字,性别等等的每一条信息,然后Claim组成一个 ...