一、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的使用的更多相关文章

  1. spring boot(四):thymeleaf使用详解

    在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4. ...

  2. Thymeleaf

    1.在html顶部添加 <html xmlns:th="http://www.thymeleaf.org"> 2.url表达式 @{...} <link rel= ...

  3. Thymeleaf 模板的使用

    Thymeleaf是现代化服务器端的Java模板引擎,不同与JSP和FreeMarker,Thymeleaf的语法更加接近HTML,并且也有不错的扩展性.详细资料可以浏览官网.本文主要介绍Thymel ...

  4. vert.x学习(三),Web开发之Thymeleaf模板的使用

    在vert.x中使用Thymeleaf模板,需要引入vertx-web-templ-thymeleaf依赖.pom.xml文件如下 <?xml version="1.0" e ...

  5. 页面上使用 Thymeleaf 的内联js不当造成了 java.lang.StackOverflowError: null 问题

    由于在页面上内联js使用不当,从而在从 Controller 跳转到页面时发生了以下错误: java.lang.StackOverflowError: null at org.thymeleaf.ut ...

  6. Thymeleaf 与 Javascript

    在 javascript 代码中使用 Thymeleaf 模板引擎: <script th:inline="javascript"> $("#content& ...

  7. Thymeleaf+SpringMVC,如何从模板中获取数据

    Thymeleaf+SpringMVC,如何从模板中获取数据 在一个典型的SpringMVC应用中,带@Controller注解的类负责准备数据模型Map的数据和选择一个视图进行渲染.这个模型Map对 ...

  8. Thymeleaf+Spring整合

    前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...

  9. thymeleaf常用标签

    1. th:checked ,th:selected标签<input type="radio" value="M" name="gender&q ...

  10. thymeleaf的常见用法

    1,th:属性名="",就可以直接修改控件的属性,比如 <input th:type="button" th:name="xinxin" ...

随机推荐

  1. 【flutter学习】基础知识(一)

    今天开始学习一下flutter 学习思路:首先由一个简单的例子引出每次学习的对象,一点一点加入元素,针对于代码去了解学习详细知识. 看完本篇博客能够快速的读懂flutter简单代码. flutter ...

  2. intellij idea的Maven项目运行报程序包找不到的错误

    概括一句话:IDEA的Terminal命令行输入mvn idea:idea或者mvn idea:module , 即可解决 重做过开发环境后在intellij idea中载入java工程,通过mave ...

  3. L SERVER 数据库被标记为“可疑”的解决办法

    问题背景: 日常对Sql Server 2005关系数据库进行操作时,有时对数据库(如:Sharepoint网站配置数据库名Sharepoint_Config)进行些不正常操作如数据库在读写时而无故停 ...

  4. 一些代码小技巧&经典代码

    请说明逻辑与(&&)在下边表达式中起到的重要作用 count != 0 && sum/count 答:该表达式使用逻辑与(&&)来确保 sum/coun ...

  5. Introduction to x265 Rate Control Algorithm

    The rate control in x265 is the same as x264's implementation, which is mostly empirical. It include ...

  6. Vue | 指令实现自动翻译填充英文名功能

    背景:应用系统中存在多个创建实体表单,表单填写时,在填写中文名称后,要填写对应的英文名作为标识或数据库查询索引. 需求:填写中文名的同时,系统自动生成英文名并填充到表单中,辅助用户操作,节约操作时间. ...

  7. [bug] Your password does not satisfy the current policy requirements

    参考 https://blog.csdn.net/zhanaolu4821/article/details/93622812

  8. Linux 操作系统(三) 添加用户、切换用户、删除用户

    以下命令均已在 Kali Linux 验证. 1.添加用户 --1-- useradd -m username            //username 代表你所添加的用户名 --2-- passw ...

  9. Linux进阶之TCP三次握手四次挥手

    TCP(Transfer control protocol)传输控制协议 一.两种传输模式: TCP面向有连接 可靠 常用于点对点 微信 UDP面向无连接 高速 常用于点对面 直播 二.数据方向: 在 ...

  10. JavaEE 三层架构模式

    什么是架构模式 要理解三层架构模式,我们得先搞清楚什么是架构模式.(这里说的架构模式是针对后端开发) 所谓架构就是系统最高级别的设计,一个系统特别复杂时才需要架构设计,如果只是开发一个很小程序,就谈不 ...