Thymeleaf语法总结 | 笔记分享
Thymeleaf语法总结
一、Thymeleaf介绍
Thymeleaf是Spring boot推荐使用的模版引擎,直接以html显示,前后端可以很好的分离。
二、Thymeleaf语法(Thymeleaf3)
<h1 th:text="${session.vel2}"></h1>
//每次遍历都会产生th:each所标注的标签以及内部标签
<tr th:each="user : ${users}">
<td th:text="${user.name}">Onions</td>
<td th:text="${user.age}">2.41</td>
</tr>
<div th:if="true">
你填的是true
</div>
//这里引用了一个th:if指令,跟vue中的v-if类似
<h2 th:object="${user}">
<p>Name: <span th:text="*{name}">Jack</span>.</p>
<p>Age: <span th:text="*{age}">21</span>.</p>
<p>friend: <span th:text="*{friend.name}">Rose</span>.</p>
</h2>
前端HTML
<!DOCTYPE html><!--名称空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Thymeleaf 语法</title>
</head>
<body>
<h2>ITDragon Thymeleaf 语法</h2>
<!--th:text 设置当前元素的文本内容,常用,优先级不高-->
<p th:text="${thText}" />
<p th:utext="${thUText}" /> <!--th:value 设置当前元素的value值,常用,优先级仅比th:text高-->
<input type="text" th:value="${thValue}" /> <!--th:each 遍历列表,常用,优先级很高,仅此于代码块的插入-->
<!--th:each 修饰在div上,则div层重复出现,若只想p标签遍历,则修饰在p标签上-->
<div th:each="message : ${thEach}"> <!-- 遍历整个div-p,不推荐-->
<p th:text="${message}" />
</div>
<div> <!--只遍历p,推荐使用-->
<p th:text="${message}" th:each="message : ${thEach}" />
</div> <!--th:if 条件判断,类似的有th:switch,th:case,优先级仅次于th:each, 其中#strings是变量表达式的内置方法-->
<p th:text="${thIf}" th:if="${not #strings.isEmpty(thIf)}"></p> <!--th:insert 把代码块插入当前div中,优先级最高,类似的有th:replace,th:include,~{} :代码块表达式 -->
<div th:insert="~{grammar/common::thCommon}"></div> <!--th:object 声明变量,和*{} 一起使用-->
<div th:object="${thObject}">
<p>ID: <span th:text="*{id}" /></p><!--th:text="${thObject.id}"-->
<p>TH: <span th:text="*{thName}" /></p><!--${thObject.thName}-->
<p>DE: <span th:text="*{desc}" /></p><!--${thObject.desc}-->
</div>
</body>
</html>
后台controller:
@Controller
public class ThymeleafController { @RequestMapping("thymeleaf")
public String thymeleaf(ModelMap map) {
map.put("thText", "th:text 设置文本内容 <b>加粗</b>");
map.put("thUText", "th:utext 设置文本内容 <b>加粗</b>");
map.put("thValue", "thValue 设置当前元素的value值");
map.put("thEach", Arrays.asList("th:each", "遍历列表"));
map.put("thIf", "msg is not null");
map.put("thObject", new ThObject(1L, "th:object", "用来偷懒的th属性"));
return "grammar/thymeleaf";
}
}
2、标准表达式语法:
- ${...} 变量表达式,Variable Expressions
- @{...} 链接表达式,Link URL Expressions
- #{...} 消息表达式,Message Expressions
- ~{...} 代码块表达式,Fragment Expressions
- *{...} 选择变量表达式,Selection Variable Expressions
~{...} 代码块表达式,支持两种语法结构
代码块表达式的使用
#{...} 消息表达式
@{...} 链接表达式
<link th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
<link th:href="@{/main/css/itdragon.css}" rel="stylesheet">
<form class="form-login" th:action="@{/user/login}" th:method="post" >
<a class="btn btn-sm" th:href="@{/login.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/login.html(l='en_US')}">English</a>
${...} 变量表达式功能
常用的内置对象
常用的内置方法
1 <!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><head>
2 <meta charset="UTF-8">
3 <title>ITDragon Thymeleaf 内置方法</title></head><body>
4 <h2>ITDragon Thymeleaf 内置方法</h2>
5 <h3>#strings </h3>
6 <div th:if="${not #strings.isEmpty(itdragonStr)}" >
7 <p>Old Str : <span th:text="${itdragonStr}"/></p>
8 <p>toUpperCase : <span th:text="${#strings.toUpperCase(itdragonStr)}"/></p>
9 <p>toLowerCase : <span th:text="${#strings.toLowerCase(itdragonStr)}"/></p>
10 <p>equals : <span th:text="${#strings.equals(itdragonStr, 'itdragonblog')}"/></p>
11 <p>equalsIgnoreCase :
12 <span th:text="${#strings.equalsIgnoreCase(itdragonStr, 'itdragonblog')}"/></p>
13 <p>indexOf : <span th:text="${#strings.indexOf(itdragonStr, 'r')}"/></p>
14 <p>substring : <span th:text="${#strings.substring(itdragonStr, 2, 8)}"/></p>
15 <p>replace : <span th:text="${#strings.replace(itdragonStr, 'it', 'IT')}"/></p>
16 <p>startsWith : <span th:text="${#strings.startsWith(itdragonStr, 'it')}"/></p>
17 <p>contains : <span th:text="${#strings.contains(itdragonStr, 'IT')}"/></p>
18 </div>
19
20 <h3>#numbers </h3>
21 <div>
22 <p>formatDecimal 整数部分随意,小数点后保留两位,四舍五入:
23 <span th:text="${#numbers.formatDecimal(itdragonNum, 0, 2)}"/></p>
24 <p>formatDecimal 整数部分保留五位数,小数点后保留两位,四舍五入:
25 <span th:text="${#numbers.formatDecimal(itdragonNum, 5, 2)}"/></p>
26 </div>
27
28 <h3>#bools </h3>
29 <div th:if="${#bools.isTrue(itdragonBool)}">
30 <p th:text="${itdragonBool}"></p>
31 </div>
32
33 <h3>#arrays </h3>
34 <div th:if="${not #arrays.isEmpty(itdragonArray)}">
35 <p>length : <span th:text="${#arrays.length(itdragonArray)}"/></p>
36 <p>contains : <span th:text="${#arrays.contains(itdragonArray, 5)}"/></p>
37 <p>containsAll : <span th:text="${#arrays.containsAll(itdragonArray, itdragonArray)}"/></p>
38 </div>
39 <h3>#lists </h3>
40 <div th:if="${not #lists.isEmpty(itdragonList)}">
41 <p>size : <span th:text="${#lists.size(itdragonList)}"/></p>
42 <p>contains : <span th:text="${#lists.contains(itdragonList, 0)}"/></p>
43 <p>sort : <span th:text="${#lists.sort(itdragonList)}"/></p>
44 </div>
45 <h3>#maps </h3>
46 <div th:if="${not #maps.isEmpty(itdragonMap)}">
47 <p>size : <span th:text="${#maps.size(itdragonMap)}"/></p>
48 <p>containsKey : <span th:text="${#maps.containsKey(itdragonMap, 'thName')}"/></p>
49 <p>containsValue : <span th:text="${#maps.containsValue(itdragonMap, '#maps')}"/></p>
50 </div>
51 <h3>#dates </h3>
52 <div>
53 <p>format : <span th:text="${#dates.format(itdragonDate)}"/></p>
54 <p>custom format : <span th:text="${#dates.format(itdragonDate, 'yyyy-MM-dd HH:mm:ss')}"/></p>
55 <p>day : <span th:text="${#dates.day(itdragonDate)}"/></p>
56 <p>month : <span th:text="${#dates.month(itdragonDate)}"/></p>
57 <p>monthName : <span th:text="${#dates.monthName(itdragonDate)}"/></p>
58 <p>year : <span th:text="${#dates.year(itdragonDate)}"/></p>
59 <p>dayOfWeekName : <span th:text="${#dates.dayOfWeekName(itdragonDate)}"/></p>
60 <p>hour : <span th:text="${#dates.hour(itdragonDate)}"/></p>
61 <p>minute : <span th:text="${#dates.minute(itdragonDate)}"/></p>
62 <p>second : <span th:text="${#dates.second(itdragonDate)}"/></p>
63 <p>createNow : <span th:text="${#dates.createNow()}"/></p>
64 </div></body></html>
1 @RequestMapping("varexpressions")public String varexpressions(ModelMap map) {
2 map.put("itdragonStr", "itdragonBlog");
3 map.put("itdragonBool", true);
4 map.put("itdragonArray", new Integer[]{1,2,3,4});
5 map.put("itdragonList", Arrays.asList(1,3,2,4,0));
6 Map itdragonMap = new HashMap();
7 itdragonMap.put("thName", "${#...}");
8 itdragonMap.put("desc", "变量表达式内置方法");
9 map.put("itdragonMap", itdragonMap);
10 map.put("itdragonDate", new Date());
11 map.put("itdragonNum", 888.888D); return "grammar/varexpressions";
12 }
Thymeleaf语法总结 | 笔记分享的更多相关文章
- C#面试题(转载) SQL Server 数据库基础笔记分享(下) SQL Server 数据库基础笔记分享(上) Asp.Net MVC4中的全局过滤器 C#语法——泛型的多种应用
C#面试题(转载) 原文地址:100道C#面试题(.net开发人员必备) https://blog.csdn.net/u013519551/article/details/51220841 1. . ...
- 1C课程笔记分享_StudyJams_2017
课程1C 概述 课程1C是创建一个生日贺卡应用的实践课程,所以本篇笔记分享主要记录个人的实践过程,此外分享一些比较零散的知识点. Drawable文件夹 Drawable文件夹是Android项目统一 ...
- Golang 语法学习笔记
Golang 语法学习笔记 包.变量和函数. 包 每个 Go 程序都是由包组成的. 程序运行的入口是包 main. 包名与导入路径的最后一个目录一致."math/rand" 包由 ...
- MarkDown语法 学习笔记 效果源码对照
MarkDown基本语法学习笔记 Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. 下面将对Markdown的基本使用做一个介绍 目 ...
- 毕业设计 之 五 PHP语法学习笔记
毕业设计 之 四 PHP语法学习笔记 作者:20135216 平台:windows10 软件:XAMPP,DreamWeaver 说明:该笔记是对网站编程语言的详细学习 一.PHP基础 0. 关于环境 ...
- uml精粹——11.活动图(及整个读书笔记分享)
11.活动图activity diagram 活动图是描写叙述过程化逻辑procedural logic.业务过程business process和工作流work flow的技术. 他和流程图fl ...
- thymeleaf模板、thymeleaf语法相关中文文档教程
thymeleaf模板在SpringBoot中是默认的模范引擎技术,SpringBoot不推荐使用比较老旧的jsp.但如果您想使用jsp的话,当然也可以.我这里为您讲述thymeleaf模板的基本th ...
- JavaScript基础——JavaScript语法基础(笔记)
JavaScript语法基础(笔记) 1.语言编码 JavaScript语言建立在Unicode字符集基础之上,因此脚本中,用户可以使用双字节的字符命名常量.变量或函数等. [示例] var 我=&q ...
- Thymeleaf语法总结
Thymeleaf是Spring boot推荐使用的模板引擎. 一.th属性 html有的属性,Thymeleaf基本都有,而常用的属性大概有七八个.其中th属性执行的优先级从1~8,数字越低优先级越 ...
随机推荐
- 关于Django的序列化问题。serializers
在DRF框架里,ModelSerializers是一个重要的组件.大大的帮组我们节省了数据序列化的过程,真的可以说是良心产品.接手的这个项目用的Django,前人的代码都是手动序列化的,为了保证风格的 ...
- 区块链学习7:超级账本项目Hyperledger与Fabric以及二者的关系
☞ ░ 前往老猿Python博文目录 ░ 一.超级账本(hyperledger) 超级账本(hyperledger)是Linux基金会于2015年发起的推进区块链数字技术和交易验证的开源项目,成员包括 ...
- (四)一个bug的生命周期
Bug的属性 Bug重现环境 这个应该是我们重现BUG的一个前提,如果没有这个前提,我们可能会无法重现问题,或者根本就无从下手. • 操作系统 这个是一般软件运行的一大前提,基本上所有的软件都依赖于操 ...
- Symbol类型是不可枚举的
const info = { [Symbol('a')]: 'b' } console.log(info)//{Symbol('a'): 'b'} console.log(Object.keys(in ...
- Robot Framework+adb框架自动化测试Android设备案例⑷——L2层关键字
一.EMMC测试套件 L2层关键字.robot *** Settings *** Resource ../L3公共层.robot *** Keywords *** 一般录影文件列表(EMMC) ${f ...
- BIOS、UEFI、Boot Loader都是些什么
BIOS.UEFI.Boot Loader都是些什么 目录 BIOS.UEFI.Boot Loader都是些什么 什么是BIOS 基本的输入输出是什么 自检程序"检"了什么 系统自 ...
- C++ 消失的析构函数 —— virtual 实现的动态析构
在C++类的结构中可以使用类方法创建内存,使用类的析构函数去施放内存,但有这么一种情况会导致:即使在析构函数中释放了内存,但由于析构函数没有被调用而导致内存泄漏,如下代码. 1 #include &l ...
- 【UV统计】海量数据统计的前世今生
转载请注明出处 背景 在互联网公司中,每个项目都需要数据统计.分析,便于项目组利用详细数据研究项目的整体情况,进行下一步的调整.在数据统计中,UV统计是最常见的,也是最普遍的.有的场景要求实时性很高, ...
- JWT 注册登录
1.JWT安装配置 pip install djangorestframework-jwt==1.11.0 1.2 syl/settings.py 配置jwt载荷中的有效期设置 # jwt载荷中的有效 ...
- Linux安装Mysql8.0.20并配置主从复制(一主一从,双主双从)
1. 主从复制解释 将主数据库的增删改查等操作记录到二进制日志文件中,从库接收主库日志文件,根据最后一次更新的起始位置,同步复制到从数据库中,使得主从数据库保持一致. 2. 主从复制的作用 高可用 ...