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