thymeleaf——th:each、th:if的使用
一、th:each
作用:用于遍历controller层发送过来的集合。
例:
Controller代码:
@Controller
public class HelloController {
@RequestMapping("/success")
public String success(Map<String,Object> map){
map.put("users", Arrays.asList("张三","李四","王五"));
return "success";
} }
下面我们通过th:each属性在html页面将其遍历显示出来
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h4 th:text="${user}" th:each="user:${users}"></h4>
</body>
</html>
讲解:
th:each="user:${users}"
其中${users}是将取出名为users的List集合,每次遍历取出List集合中的一个元素赋值给user
注意:th:each每次遍历都会生成一个包含它的标签,如我们举的这个例子,users中一共有三个元素,所以会遍历三次,每次都会生成一个h4标签
二、th:if
Thymeleaf 的条件判断是 通过 th:if 来做的,只有为真的时候,才会显示当前元素
<p th:if="${testBoolean}" >如果testBoolean 是 true ,本句话就会显示</p>
取反可以用not, 或者用th:unless.
<p th:if="${not testBoolean}" >取反 ,所以如果testBoolean 是 true ,本句话就不会显示</p>
<p th:unless="${testBoolean}" >unless 等同于上一句,所以如果testBoolean 是 true ,本句话就不会显示</p>
除此之外,三元表达式也比较常见
<p th:text="${testBoolean}?'当testBoolean为真的时候,显示本句话,这是用三相表达式做的':''" ></p>
thymeleaf——th:each、th:if的使用的更多相关文章
- spring boot(四):thymeleaf使用详解
在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4. ...
- Thymeleaf
1.在html顶部添加 <html xmlns:th="http://www.thymeleaf.org"> 2.url表达式 @{...} <link rel= ...
- Thymeleaf 模板的使用
Thymeleaf是现代化服务器端的Java模板引擎,不同与JSP和FreeMarker,Thymeleaf的语法更加接近HTML,并且也有不错的扩展性.详细资料可以浏览官网.本文主要介绍Thymel ...
- vert.x学习(三),Web开发之Thymeleaf模板的使用
在vert.x中使用Thymeleaf模板,需要引入vertx-web-templ-thymeleaf依赖.pom.xml文件如下 <?xml version="1.0" e ...
- 页面上使用 Thymeleaf 的内联js不当造成了 java.lang.StackOverflowError: null 问题
由于在页面上内联js使用不当,从而在从 Controller 跳转到页面时发生了以下错误: java.lang.StackOverflowError: null at org.thymeleaf.ut ...
- Thymeleaf 与 Javascript
在 javascript 代码中使用 Thymeleaf 模板引擎: <script th:inline="javascript"> $("#content& ...
- Thymeleaf+SpringMVC,如何从模板中获取数据
Thymeleaf+SpringMVC,如何从模板中获取数据 在一个典型的SpringMVC应用中,带@Controller注解的类负责准备数据模型Map的数据和选择一个视图进行渲染.这个模型Map对 ...
- Thymeleaf+Spring整合
前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...
- thymeleaf常用标签
1. th:checked ,th:selected标签<input type="radio" value="M" name="gender&q ...
- thymeleaf的常见用法
1,th:属性名="",就可以直接修改控件的属性,比如 <input th:type="button" th:name="xinxin" ...
随机推荐
- docker文件系统分层存储原理
一,前言 众所周知,docker镜像技术的基础是联合文件系统(UnionFS),其文件系统是分层的,那它的分层机制是什么样的呢?共分为几种层呢?又是怎么工作的呢? 目前docker支持的联合文件系统有 ...
- Thyemleaf报错: Method call: Attempted to call method *** on null context object
翻译:方法调用:尝试在null上下文对象上调用方法*** 解释:在Thyemleaf上下中不存在所要调用的对象,相当于Java代码中的NullPointerException 解决方案: 方案1. 需 ...
- 【maven和jdk】报错:系统找不到指定的文件
创建一个maven项目出错 问题描述 在idea.log出现如下错误(系统找不到指定的文件,但是不知道指定文件是什么) com.intellij.execution.process.ProcessNo ...
- Sublime 快捷生成HTML 插件安装
更多精彩关注公众号 1 安装 Package Control1.1 ctrl + ` 呼出控制台1.2 复制(不要带最外层的双引号,该代码仅适用于sublime text 3)"import ...
- linux命令的使用 以及基本docker命令及docker镜像安装
以linux CentOS-7 64位 系统为例 查看ip ifconfig 固定ip 输入vim /etc/sysconfig/network-scripts/ifcfg-ens3 其中vim是修 ...
- windows调起git bash执行sh脚本定时统计git仓库代码量
本来挺简单的一个东西硬是弄了两天 心力交瘁 找了网上不少资料 整理一下发给大家 首先是统计每个人的代码量的git命令 在网上找的 我这里做了以下修改 git log --format='%aN'|so ...
- 二分查找确定lower_bound和upper_bound
lower_bound当target存在时, 返回它出现的第一个位置,如果不存在,则返回这样一个下标i:在此处插入target后,序列仍然有序. 代码如下: int lower_bound(int* ...
- golang:指针理解总结
指针的定义 指针是一个代表着某个内存地址的值.这个内存地址往往是在内存中存储的另一个变量的值的起始位置. go指针是提供操作数据的基本桥梁.因为go很多调用,往往复制一份对象,例如函数的参数,如果没有 ...
- [Python] tkinter 之 Listbox & Combobox
示例: 1 #用户界面 2 import os 3 os.chdir('F:\\spyder_workspace\\ColCal') 4 import Main 5 from tkinter impo ...
- CentOS 7系统中的时间日期设置
修改 CentOS 7系统中的时间日期设置 timedatectl set-ntp no timedatectl timedatectl set-time 2022-06-04 timedatectl ...