1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-each项目

2. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bjbs</groupId><artifactId>spring-boot-view-thymeleaf-each</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version></parent><!-- 修改jdk版本 --><properties><java.version>1.8</java.version><!-- 指定thymeleaf和thymeleaf-layout-dialect高版本可以防止html标签不规范报错 --><thymeleaf.version>3.0.2.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version></properties><dependencies><!-- springBoot的启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies>
</project>

3. Thymeleaf迭代List状态变量属性

3.1. index: 当前迭代器的索引, 从0开始。

3.2. count: 当前迭代对象的计数, 从1开始。

3.3. size: 被迭代对象的长度。

3.4. even/odd: 布尔值, 当前循环是否是偶数/奇数, 从1开始。

3.5. first: 布尔值, 当前循环的是否是第一条, 如果是返回true, 否则返回false。

3.6. last: 布尔值, 当前循环的是否是最后一条, 如果是则返回true, 否则返回false。

4. 在src/main/resources/templates下新建eachList.html

<!DOCTYPE html>
<html><head><meta charset="UTF-8" /><title>Thymeleaf迭代List</title></head><body><table border="1"><tr><th>id</th><th>姓名</th><th>年龄</th><th>索引</th><th>计数</th><th>长度</th><th>偶数</th><th>奇数</th><th>是否是第一条</th><th>是否是最后一条</th></tr><tr th:each="user,status : ${userList}"><td th:text="${user.id}"></td><td th:text="${user.name}"></td><td th:text="${user.age}"></td><td th:text="${status.index}"></td><td th:text="${status.count}"></td><td th:text="${status.size}"></td><td th:text="${status.even}"></td><td th:text="${status.odd}"></td><td th:text="${status.first}"></td><td th:text="${status.last}"></td></tr></table></body>
</html>

5. 在src/main/resources/templates下新建eachMap.html

<!DOCTYPE html>
<html><head><meta charset="UTF-8" /><title>Thymeleaf迭代Map</title></head><body><table border="1"><tr><th>user对象</th></tr><tr th:each="user : ${userMap}"><td th:text="${user}"></td></tr></table><table border="1"><tr><th>id</th><th>姓名</th><th>年龄</th></tr><tr th:each="user : ${userMap}"><td th:each="entry:${user}" th:text="${entry.value.id}" ></td><td th:each="entry:${user}" th:text="${entry.value.name}"></td><td th:each="entry:${user}" th:text="${entry.value.age}"></td></tr></table></body>
</html>

6. 新建User.java

package com.bjbs.pojo;import java.io.Serializable;public class User implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String name;private Integer age;public User() {}public User(Integer id, String name, Integer age) {this.id = id;this.name = name;this.age = age;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}

7. 新建UserController.java

package com.bjbs.controller;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bjbs.pojo.User;@Controller
public class UserController {@RequestMapping("/eachList")public String eachList(Model model) {List<User> list = new ArrayList<User>();list.add(new User(1, "张三", 20));list.add(new User(2, "李四", 22));list.add(new User(3, "王五", 24));list.add(new User(4, "赵六", 18));list.add(new User(5, "李师师", 16));model.addAttribute("userList", list);return "eachList";}@RequestMapping("/eachMap")public String eachMap(Model model) {Map<String, User> map = new HashMap<String, User>();map.put("u1", new User(1, "张三", 20));map.put("u2", new User(2, "李四", 22));map.put("u3", new User(3, "王五", 24));model.addAttribute("userMap", map);return "eachMap";}
}

8. 新建App.java

package com.bjbs;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** SpringBoot启动类*/
@SpringBootApplication
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}
}

9. 启动项目, 并使用浏览器访问迭代List

10. 启动项目, 并使用浏览器访问迭代Map

013_SpringBoot视图层技术thymeleaf-迭代遍历相关推荐

  1. 010_SpringBoot视图层技术thymeleaf-变量输出与字符串操作

    一. Thymeleaf变量输出 1. th:text在页面中输出变量. 2. th:value将变量输出到input标签的value中. 二. Thymeleaf字符串操作 1. Thymeleaf ...

  2. 015_SpringBoot视图层技术thymeleaf-URL表达式

    1. url表达式: th:href和th:src. 2. url表达式基本语法: @{}. 3. 绝对路径: <a th:href="@{http://www.baidu.com}& ...

  3. 014_SpringBoot视图层技术thymeleaf-访问域对象

    1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-scope项目 2. pom.xml <project xmlns="http: ...

  4. 012_SpringBoot视图层技术thymeleaf-条件判断

    1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-if-switch项目 2. pom.xml <project xmlns="h ...

  5. 011_SpringBoot视图层技术thymeleaf-日期格式化

    一. 日期格式化 1. ${#dates.format(birthday)}格式化日期, 默认的以浏览器默认语言为格式化标准. 2. ${#dates.format(birthday,'yyy/MM/ ...

  6. 008_SpringBoot视图层技术jsp

    1. 使用maven构建SpringBoot的名叫spring-boot-view-jsp项目 2. pom.xml <project xmlns="http://maven.apac ...

  7. 服务器渲染技术-Thymeleaf

    1.基本介绍 Thymeleaf 是什么 Thymeleaf是一个现代的服务器端Java模板引擎,适用于Web和独立环境,能够处理HTML,XML,JavaScript,CSS甚至纯文本 Thymel ...

  8. 微信小程序逻辑层视图层解析

    框架 小程序开发框架的目标是通过尽可能简单.高效的方式让开发者可以在微信中开发具有原生 APP 体验的服务.框架提供了自己的视图层描述语言 WXML 和 WXSS,以及基于JavaScript的逻辑层 ...

  9. 【微信小程序】一文带你吃透小程序开发框架——视图层中的事件系统

    0️⃣前言 事件是视图层到逻辑层的通讯方式. 事件可以将用户的行为反馈到逻辑层进行处理. 事件可以绑定在组件上,当达到触发事件,就会执行逻辑层中对应的事件处理函数. 事件对象可以携带额外信息,如 id ...

最新文章

  1. MIT警告深度学习正在逼近计算极限,网友:放缓不失为一件好事
  2. 撞库攻击:一场需要用户参与的持久战
  3. 计算机游戏的英语怎么写,电脑游戏英语怎么写
  4. 游戏AI研究(三):路径规划
  5. tpch测试mysql_MySQL-tpch 测试工具简要手册
  6. 小白兔想的飞鸽传书(173dmba)安卓版
  7. 2场直播丨MySQL 数据库最常见的 6 类故障的排除方法、2020数据技术嘉年华·金融峰会暨数据库大咖讲坛(第4期)...
  8. [傅里叶变换及其应用学习笔记] 三十. 拉东变换
  9. centos安装python3.6.3、pip_Centos 7安装python3和pip
  10. eclipse—安装ADT插件搭建安卓开发环境
  11. python3.8安装cartopy使用报错:DLL load failed while importing trace
  12. java 微信定位到市_java 微信公众号地理位置获取
  13. pyinstaller打包执行文件报错NameError: name ‘defaultParams‘ is not defined问题解决方案
  14. 云栖科技评论第70期:数字时代需要双螺旋
  15. 1 简历该怎么写?注意事项--绝密,程序员大厂面试求职大揭秘!
  16. Springboot启动报错[ main] o.s.boot.SpringApplication: Application run failed(佷有可能是版本问题)
  17. linux设备驱动之USB数据传输分析 usb_submit_urb
  18. Project ERROR: Cannot run compiler 'cl'. Maybe you forgot to setup the environment?
  19. 危机,熊猫烧香进入手机!!!
  20. 十大最佳 Linux 服务器发行版

热门文章

  1. 【10大专利看iPhone未来】全息图、虚拟卷轴,移动AR……苹果还有哪些黑科技?...
  2. (转载)jQuery 1.6 源码学习(一)——core.js[1]之基本架构
  3. Chapter 1 First Sight——9
  4. 定时任务:Java中Timer和TimerTask的使用
  5. solaris10找安装包的地方
  6. Android——Intent动作汇总(转)
  7. 如何扩展CentOS7的SWAP分区
  8. IplImage和Mat间的相互转换
  9. javascript 数字格式化
  10. 【转】指令周期,机器周期,时钟周期,振荡周期有什么关系