Thymeleaf模板入门(三)
Thymeleaf基本语法
1. 往期
Thymeleaf模板入门(一)
Thymeleaf模板入门(二)
2. 案例
实体类User
package top.infinxkj.thymeleaf.pojo;public class User {public String name;public int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
控制器
@Controller
public class DemoController {@GetMapping("my")public String myIndex(Model model){User user = new User();user.setName("张 三");user.setAge(28);model.addAttribute("user",user);return "index";}
}
动静结合
Thymeleaf中所有的表达式都需要写在指令中,指令时HTML5的自定义属性,在静态环境下,Thymeleaf表达式的内容会被浏览器自动忽略。
${user.name}
可以写作${user['name']}
3. 自定义变量
在数据量较多时,频繁使用对象.属性名会比较繁琐,我们可以自定义变量。
<div th:object="${user}"><p>name: <span th:text="*{name}">大古</span></p><p>age: <span th:text="*{age}">28</span></p>
</div>
- 用
th:object="${user}"
获取user的值 - 在内部的任意元素上,可以通过
*{属性名}
,来获取user中的属性
4. 方法
ognl表达式本身支持方法调用
<div th:object="${user}"><p>FirstName: <span th:text="*{name.split(' ')[0]}">Jack</span>.</p><p>LastName: <span th:text="*{name.split(' ')[1]}">Li</span>.</p>
</div>
Thymeleaf提供了很多内置对象和对象内的方法,详情见:Thymeleaf模板入门二
举例:
controller添加
model.addAttribute("today",new Date());
HTML页面:
<p>今天是:<span th:text="${#dates.format(today,'yyyy-MM-dd')}">2021-04-03</span>
</p>
5. 字面值
有时,我们需要在指令中填写基本类型如(字符串、数值、布尔等),不希望被Thymeleaf解析为变量,这个时候称字面值。
字符串字面值
用对单引号包括
<p>字符串字面值:<span th:text="'name'">字面值</span>
</p>
数字字面值
不许任何符号,直接书写,甚至可以进行算术运算
<p>数字字面值:小明身高: <span th:text="185">190</span>.
</p>
布尔字面值
布尔类型的字面值是true或false
<p>布尔型字面值<div th:if="true">true</div>
</p>
6. 拼接
普通字符串与表达式拼接:
<span th:text="'欢迎您:' + ${user.name} + '!'"></span>
字符串字面值需要用''
,Thymeleaf使用一对|
即可:
<span th:text="|欢迎您:${user.name}|"></span>
7. 运算
${}
内部是通过OGNL表达式引擎解析的,外部的是通过Thymeleaf的引擎解析,运算符尽量放在${}
外进行。
算术运算
支持的算术运算符:+ - * / %
<span th:text="${user.age}"></span>
<span th:text="${user.age}%2 == 0"></span>
比较运算
支持:>
, <
, >=
and <=
,但是>
, <
不能直接使用,xml会解析为标签,要使用别名。
注意 ==
and !=
不仅可以比较数值,类似于equals的功能。
可以使用的别名:gt (>), lt (<), ge (>=), le (<=), not (!). Also eq (==), neq/ne (!=).
条件运算
- 三元运算
<span th:text="${user.age}>=18 ? '成年':'未成年'"></span>
三元运算符的三个部分:conditon ? then : else
condition:条件,then:条件成立的结果,else:不成立的结果
其中的每部分都可是Thymeleaf中的任意表达式。
默认值
有的时候,我们取一个值可能为空,这个时候需要做非空判断,可以使用
表达式 ?: 默认值
简写:
<span th:text="${user.name} ?: '李华'"></span>
表达式值为null时,使用默认值。注意:?:
之间没有空格。
8. 循环
循环是频繁使用的,使用th:each
指令来完成:
有用户的集合:users在Context中。
<tr th:each="user : ${users}"><td th:text="${user.name}">姓名</td><td th:text="${user.age}">年龄</td>
</tr>
${users} 是要遍历的集合,可以是以下类型:
- Iterable,实现了Iterable接口的类
- Interator,迭代器
- Map,遍历得到的是Map.Entry
- Array,数组及其它一切符合数组结果的对象
- Enumeration,枚举
在迭代的同时,也可以获取迭代的状态对象:
<tr th:each="user,stat : ${users}"><td th:text="${stat.index}">序号</td><td th:text="${user.name}">姓名</td><td th:text="${user.age}">年龄</td>
</tr>
stat对象包含以下属性:
- index,从0开始的角标
- count,元素的个数,从1开始
- size,总元素个数
- current,当前遍历到的元素
- even/odd,返回是否为奇偶,boolean值
- first/last,返回是否为第一或最后,boolean值
9. 逻辑判断
Thymeleaf中使用th:if
或者 th:unless
,两者的意思相反。
<p><span th:if="${user.age} < 30">年轻人</span>
</p>
如果表达式的值为true,则标签会渲染到页面,否则不进行渲染。
以下情况被认定为true:
- 表达式值为true、为非0数值、为非0字符、为字符串,但不是
"false"
,"no"
,"off"
、不是布尔、字符串、数字、字符中的任何一种
其它情况包括null都被认定为false
10.分值控制switch
这里要使用两个指令:th:switch
和 th:case
<div th:switch="${user.name}"><p th:case="张 三">管理员</p><p th:case="李四">经理</p><p th:case="*">员工</p>
</div>
有一个th:case成立,其它的则不再判断。与java中的switch是一样的。th:case="*"
表示默认,放最后。
11. JS模板
模板引擎不仅可以渲染html,也可对JS进行预处理。为了在纯静态环境下运行,Thymeleaf代码可以被注释起来:
在script标签中通过
th:inline="javascript"
声明这是要特殊处理的js脚本语法结构:
const user = /*[[Thymeleaf表达式]]*/ "静态环境下的默认值";
表达式后面跟着的是默认值。
源码:
HTML:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>让大古编程</title>
</head>
<body>
<h3>变量</h3>
<span th:text="${user.name}">请登录</span>
<h3>自定义变量</h3>
<p>name: <span th:text="${user.name}">大骨</span></p>
<p>age: <span th:text="${user.age}">18</span></p>
<p>或者</p>
<div th:object="${user}"><p>name: <span th:text="*{name}">大古</span></p><p>age: <span th:text="*{age}">28</span></p><p>age: <span th:text="${#object.age}">28</span></p>
</div>
<h3>方法</h3>
<div th:object="${user}"><p>FirstName: <span th:text="*{name.split(' ')[0]}">Jack</span>.</p><p>LastName: <span th:text="*{name.split(' ')[1]}">Li</span>.</p>
</div>
<p>今天是:<span th:text="${#dates.format(today,'yyyy-MM-dd')}">2021-04-03</span>
</p>
</body>
<h3>字面值</h3>
<p>字符串字面值:<span th:text="'name'">字面值</span>
</p>
<p>数字字面值:小明身高: <span th:text="185">190</span>.
</p>
<p>布尔型字面值<span th:if="true">true</span>
</p>
<h3>拼接</h3>
<span th:text="'欢迎您:' + ${user.name} + '!'"></span></br>
<span th:text="|欢迎您:${user.name}|"></span>
<h3>运算</h3>
<p><span th:text="${user.age}"></span><span th:text="${user.age}%2 == 0"></span><span th:text="${user.age}>=18 ? '成年':'未成年'"></span><span th:text="${user.name} ?: '李华'"></span>
</p>
<h3>循环</h3>
<tr th:each="user : ${users}"><td th:text="${user.name}">姓名</td><td th:text="${user.age}">年龄</td>
</tr>
</br>
<tr th:each="user,stat : ${users}"><td th:text="${stat.index}">序号</td><td th:text="${user.name}">姓名</td><td th:text="${user.age}">年龄</td>
</tr>
<h3>逻辑判断</h3>
<p><span th:if="${user.age} < 30">年轻人</span>
</p>
<h3>分支控制</h3>
<div th:switch="${user.name}"><p th:case="'张 三'">管理员</p><p th:case="'李四'">经理</p><p th:case="*">员工</p>
</div>
<h3>JS模板</h3>
<script th:inline="javascript">const user = /*[[${user}]]*/ {};const age = /*[[${user.age}]]*/ 20;console.log(user);console.log(age)
</script>
</html>
Controller
package top.infinxkj.thymeleaf.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import top.infinxkj.thymeleaf.pojo.User;import java.util.*;@Controller
public class DemoController {@GetMapping("my")public String myIndex(Model model){User user = new User();User user2 = new User();user.setName("张 三");user.setAge(28);user2.setName("李四");user2.setAge(20);ArrayList<User> users=new ArrayList<User>();users.add(user);users.add(user2);model.addAttribute("user",user);model.addAttribute("users",users);model.addAttribute("today",new Date());return "index";}
}
User实体类
package top.infinxkj.thymeleaf.pojo;public class User {public String name;public int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
Thymeleaf模板入门(三)相关推荐
- Spring Boot系列之Thymeleaf模板布局
PS:原文首发于微信公众号:躬行之(jzman-blog) 前面几篇文章尝试了接口开发.Thymeleaf 模板及其常用语法,阅读本文之前可以阅读前面几篇: Spring Boot系列之开发一个接口 ...
- 玩转springboot:thymeleaf模板引擎入门程序
一.前言 常用的模板引擎有:JSP.Velocity.Freemarker.Thymeleaf 但是,Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎.而且,语法更简单,功 ...
- springboot 入门教程(4)--web开发(spring mvc和Thymeleaf模板,带源码)
2019独角兽企业重金招聘Python工程师标准>>> 首先回顾下前几篇的内容:springboot 入门教程(1),springboot 入门教程-Thymeleaf(2), sp ...
- Springboot项目搭建(三)整合thymeleaf模板
springboot整合thymeleaf模板 一.POM文件添加依赖 <!--thymeleaf--> <dependency><groupId>org.spri ...
- Marco's Java【SpringBoot入门(六) 之 Thymeleaf模板引擎的使用】
前言 本节呢给大家介绍一个新鲜 "玩意儿" 叫做Thymeleaf,Thymeleaf翻译过来就是 "百里香叶" 的意思 我发现这些大佬儿特别喜欢用叶子作为标识 ...
- java 模板引擎_SpringBoot入门系列(四)如何整合Thymeleaf模板引擎
前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...
- JavaWeb学习之路——SpringBoot 中thymeleaf模板用法(三)
thymeleaf模板用法 thymeleaf通过它特定的语法,对HTML的标记做渲染,能够访问后台的动态数据,实现静态html界面的动态化 1.添加架包 <!--引入动态模板-->< ...
- Thymeleaf模板引擎---SpringBoot
Thymeleaf模板引擎 前端交给我们的页面,是html页面.如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显 ...
- SpringBoot thymeleaf使用方法,thymeleaf模板迭代
SpringBoot thymeleaf使用方法,thymeleaf模板迭代 SpringBoot thymeleaf 循环List.Map ============================= ...
最新文章
- 解析SharedPreferences
- Linux下ACL权限控制以及用sudo设置用户对命令的执行权限
- Spark _26_Spark On Hive的配置
- 架构重构改善既有代码的设计
- 数据之路 - 数据可视化 - PowerBI工具
- 【英语天天读】Develop Your Own Helping Rituals
- LoggerFactory.getLogger
- 洛谷P2024 食物链
- 使用JS完成一个简单的计算器功能
- 华为手表广告营销案例和广告策划案例PPT模板
- MySQL 数值拼接字符串
- C++设计模式从入门到精通——实例说明
- 112家IT网络公司薪水一览表
- STM32F103_study62_The punctual atoms(Clock system initialization function analysis)
- pgadmin 4 v4.28 keeps loading
- Linux下切换capslock和control键
- 修改C盘下的用户名(适合win10和win11)
- 【Yolo】Jetson Orin Nano下部署 YoloV5
- shl and shr
- 数据结构与算法之美笔记(十四)B+树
热门文章
- 用YSlow评分插件分析我们页面
- Java:List转List (用stream实现)
- fatal: did not receive expected object
- 生物信息学在线服务器,生物信息学杂志
- thinkpad x1 carbon 5th 2017 ubuntu20.04安装指纹登录
- 视频教程-操作系统之存储管理-操作系统
- 如何用计算机把数字12变成21,计算机应用基础模拟试卷8
- C#程序员学习 Python
- 黄金分割(0.618)法求解函数极值(附代码)
- Linux中ps命令ps -aux 和ps -ef 参数解释