1. 概述
这里介绍thymeleaf的编程语法,本节主要包括如下内容
-
迭代语法:th:each; iteration status
-
条件语法:th:if; th:unless
-
switch语法:th:switch; th:case; *
下文演示以上语法的用法。
2. 演示以上语法的用法
2.1. 公共类
public class User {
private String name;
private boolean isAdmin;
private String other;
private int age;
public User(String name, boolean isAdmin, String other, int age) {
super();
this.name = name;
this.isAdmin = isAdmin;
this.other = other;
this.age = age;
ProgrammingCtl : control类
@Controller
@RequestMapping("/programming")
public class ProgrammingCtl {
@RequestMapping("programming")
public String iteration(ModelMap modeMap) {
List<User> userList = new ArrayList<User>();
userList.add(new User("son_1", true, "other_1", 11));
userList.add(new User("son_2", false, "other_2", 22));
userList.add(new User("son_3", true, "other_3", 33));
userList.add(new User("son_4", false, "other_4", 44));
modeMap.put("userList", userList);
User userIf = new User("admin", true, "other_if", 11);
modeMap.put("user", userIf);
return "programming/programming";
本请求转到页面programming.html,
2.2. 迭代语法:th:each; iteration status
常用th:each用法:
<table border="2">
<thead>
<th>name</th>
<th>age</th>
<th>isAdmin</th>
</thead>
<tbody>
<tr th:each="user : ${userList}">
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td th:text="${user.isAdmin}"></td>
</tbody>
</table>
运行结果如下:

迭代的对象
本例子中迭代的对象是java.util.List,除了List,还可以对以下对象进行迭代
- java.util.Iterable
- java.util.Enumeration
- java.util.Iterator
- java.util.Map,此时迭代返回的对象是java.util.Map.Entry
- 数组
获取迭代的中间的状态,定义在iterStat中
在迭代过程中,可以获取迭代的中间状态,详细如下:
- index :当前节点的索引,从0开始
- size : 迭代节点总数
- even/odd:当前是偶数/奇数行,boolean值
- first/last:当前是每天/最后一个元素
able border="2">
<thead>
<th>迭代索引</th>
<th>元素所处的位置索引</th>
<th>奇偶行</th>
<th>name</th>
<th>age</th>
<th>isAdmin</th>
</thead>
<tbody>
<tr th:each="user,iterStat : ${userList}">
<td th:text="${iterStat.index }"></td>
<td th:text="${iterStat.first } ? '这是第一个元素':(${iterStat.last} ? '这是最后一个元素':'')" ></td>
<td th:text="${iterStat.odd} ? 'odd' : 'even'" ></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td th:text="${user.isAdmin}"></td>
</tbody>
</table>
运行结果如下:

2.3. 条件语法:th:if; th:unless
演示如下功能
- th:if:如果值是true,则打印整个节点
- th:unless: 和th:if是相反功能,如果值为false,则打印整个节点
<span th:if="${user.isAdmin}" th:text="${user.name} + '是管理员'"> </span><br />
<span th:unless="not ${user.isAdmin}" th:text="${user.name} + '是管理员'"> </span><br />
<span>admin是管理员</span><br />
<span>admin是管理员</span><br />
th:if条件判断
除了判断boolean值外,thymeleaf还认为如下表达式为true:
- 值非空
- 值是character,但是非0
- 值是非0数字
- 值是字符串,但是不是 “false”, “off” or “no”
- 值不是boolean值,数字,character 或 字符串
2.4. switch语法:th:switch; th:case; *
演示如下功能
- th:switch / th:case
- th:case=”*” : 类似switch中的default
<div th:switch="${user.name}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="*">User is some other thing</p>
</div>
<p>User is an administrator</p>
</div>
3. 代码
代码详细见
Github
> 1. springboot自带的
thymeleaf
依赖为2.1.3版本,使用
thymeleaf
-layout-dialect版本为2以下版本。
> 2. 使用3或3以上的
thymeleaf
时,需要
thymeleaf
-layout-dialect的版本为2或以上。
> 3. 锁定
thymeleaf
版本时不能使用
thymeleaf
.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-
thymeleaf
.version标签来锁定版本。
### 3. 配置文件配置
```properties
spring.
thymeleaf
.prefix=classpath:/templates/
spring.
thymeleaf
.check-template-location=true
spring.
thymeleaf
.suffix=.html
spring.
thymeleaf
.encoding=UTF-8
spring.
thymeleaf
.content-type=text/html
spring.
thymeleaf
.mode=HTML
spring.
thymeleaf
.cache=false
> spring.
thymeleaf
.cache为缓存,需要热部署时,需要设置为false
##
语法
### 1. 替换标签体内容
```html
### 2. 替换属性
```html
### 3. 在表达式中访问属性域
```html
访问属性域
访问请求域 方式一
访问请求域 方式二
访问Session域
访Session域 方式一
访问Application域 方式一
### 4. 解析url地址
```html
解析URL地址,获取ContextPath的值
@{}是把ContextPath的值附加到指定的地址前
@{}是把ContextPath的值附加到指定的地址前
### 5. 直接执行表达式
```html
直接执行表达式
无转义效果 : [[${attrRequestScope}]]
有转义效果 : [(${attrRequestScope})]
### 6. 分支与
迭代
#### 1. if 判断
```html
if判断字符串是否为空
There is no nutrition in the blog content. After reading it, you will not only suffer from malnutrition, but also impotence.
The blog content is all parallel goods. Those who are worried about being cheated should leave quickly.
本文来自于微信,本文主要介绍了
thymeleaf
究竟是什么,spring为什么推荐用它,希望对您能有所帮助。
thymeleaf
是一种Java
模板
引擎,那何为
模板
引擎呢?
模板
引擎就是为了使用户页面和业务数据相互分离而出现的,将从后台返回的数据生成特定的格式的文档,这里说的特定格式一般都指HTML文档。它能够处理html、xml、js、css甚至纯文本,类似于freemarker。它的优点是
语法
优雅易懂、原型即页面、遵从web标准。原型即页面是它的特色,所谓原型即页面,就是你写的html,静态的去访问是什么样,动态的去访问还是这样,只不过动态的时候会把数据填充进去。1、变量表达式:${...}例如
1 th:action 定义后台控制器的路径,类似<form>标签的 action 属性,主要结合 URL 表达式,获取动态变量
2 th:method设置请求方法<form id="login" th:action="@{/login}" th:method="post">......</form>
3th:href定义超链接,主要结合 URL 表达式,获取动态变量
4th:sr...
迭代
语法
:th:each; iteration status
条件
语法
:th:if; th:unless
switch
语法
:th:
switch
; th:case; *
2、公共模拟SpringBoot 后台接口
后台逻辑(action)
// 分页查询
@RequestMapping("/allColumnValidates")
public Stri...
一旦某个 case 判断值为 true,剩余的 case 则都当做 false,“*”表示默认的 case,前面的 case 都不匹配时候,执行默认的 case。pom.xml文件,核心配置文件等文件,与文章23保持一致,本文只展示最新添加的文件,以减小文章冗余,更容易看出循环的使用方法。在springboot中使用
thymeleaf
循环。点击判断语句If和unless。当sex的值既不是m又不是w时。springboot文章23。
将
模板
中的数据和js练习到一块,这样可以在js中获取动态的数据了,相当于通过内联的
语法
格式,可以把java语言和js紧密的联合在一起了,js中使用java
模板
引擎中所提供的数据,所以说内联脚本是挺有用的。控制器
Thymeleaf
Controllerif-unless的使用。点击判断语句if和unless、(4)内联JavaScript。(4)内联JavaScript。点击判断语句
switch
。点击内联iniline。(3)内联text。(3)内联text。......
<td th:
switch
="${activity.activityState}">
<span th:case="0">未接收</span>
<span th:case="1">已接收</span>
<span th:case="2">已完成</span>
Thymeleaf
介绍
简单说,
Thymeleaf
是一个跟 Velocity、FreeMarker 类似的
模板
引擎,它可以完全替代 JSP 。相较与其他的
模板
引擎,它有如下三个极吸引人的特点:
1.
Thymeleaf
在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html ...
循环基本用法
1、对于信息页面,数据格式是一样时,页面通常都是循环
迭代
它们,写过 JSP 的 JSTL 的就知道,JSTL 有一个<c:foreach>,同理
Thymeleaf
也有一个 th:each。作用都是一...