随笔- 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. jsp 页面显示格式化的日期

    在页面引入 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 使用 ...

  2. POJ 1845 Sumdiv 【二分 || 逆元】

    任意门:http://poj.org/problem?id=1845. Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions ...

  3. 14、SpringBoot-CRUD错误处理机制(1)

    一.springboot默认的处理机制 1.浏览器返回一个错误的页面 默认处理错误:返回一个错误的页面: 包括错误类型.时间......   2.其他客户端访问 默认响应一个json数据 原理: 错误 ...

  4. REG小探

    根键名称缩写对照表   常用数据类型

  5. 火狐中jq的attr出现的bug问题用prop代替

    再工作的时候遇到一个很奇怪的问题 ,就是attr属性不好使!就问度娘去了...... 结果如下: .prop()   1..prop( propertyName ) 获取匹配集合中第一个元素的Prop ...

  6. HUD 1288 Hat's Tea(反向的贪心,非常好的一道题)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1288 Hat's Tea Time Limit: 2000/1000 MS (Java/Others)  ...

  7. HTML5——前端预处理技术(Less、Sass、CoffeeScript)

    一.Less 1.1.概要 Less是一种动态样式语言,Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展. Less 将 C ...

  8. Reading Notes : 180211 概述计算机

    读书<计算机组成原理> <鸟哥的Linux私房菜 基础篇> 本章介绍电子计算机概念以及发展历史和发展趋势,内容摘自<计算机组成原理>  <鸟哥的Linux私房 ...

  9. 更新UI放在主线程的原因

    1.在子线程中是不能进行UI 更新的,而可以立刻更新的原因是:子线程代码执行完毕了,又自动进入到了主线程,这中间的时间非常的短,让我们误以为子线程可以更新UI.如果子线程一直在运行,则无法更新UI,因 ...

  10. java中实现多线程的几种方式(简单实现)

    一.以下只是简单的实现多线程 1:继承Thread 2:实现 Runnable 3:实现callable 如果需要返回值使用callable,如果不需要返回最好使用runnable,因为继承只能单继承 ...