SpringBoot——Thymeleaf中使用th:each遍历数组、List、Map

1.写在前面

这个属性非常常用,比如从后台传来一个对象集合那么就可以使用此属性遍历输出,它与JSTL 中的<c: forEach>类似,此属性既可以循环遍历集合,也可以循环遍历数组及 Map 。


2.应用举例

2.1 遍历数组

首先,我们准备一个 model 类。

这里使用了 lombok 下的一个注解 @Data,它可以帮助我们自动生成 get/set 方法、toString()等。但是我们在这个类中是看不到的,查看的方法是:点击这个类,然后按 Alt + 7,即可查看。

package com.songzihao.springboot.model;

import lombok.Data;

/**
 *
 */
@Data
public class User {

    private Integer id;
    private String name;
    private String phone;
    private String address;

}

之后,我们写一个控制层,其中有一个请求方法。

package com.songzihao.springboot.controller;

import com.songzihao.springboot.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
public class UserController {

    @RequestMapping(value = "/each/array")
    public String eachArray(Model model) {
        User[] userArray = new User[10];
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(i);
            user.setName("宋小" + i);
            user.setPhone("1853790565" + i);
            user.setAddress("洛阳市涧西区XX路" + i + "号");
            userArray[i] = user;
        }
        model.addAttribute("userArray",userArray);
        return "eachArray";
    }
}

然后是这个请求方法对应的html页面。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>循环遍历Array数组</title>
</head>
<body>
    <div th:each="user,userStat:${userArray}">
        <span th:text="${userStat.index}"></span>
        <span th:text="${user.id}"></span>
        <span th:text="${user.name}"></span>
        <span th:text="${user.phone}"></span>
        <span th:text="${user.address}"></span>
    </div>
</body>
</html>

最后在核心配置文件中添加以下内容,启动入口类,测试。

#关闭Thymeleaf页面的缓存开关
spring.thymeleaf.cache=false

#thymeleaf 模版前缀,默认值,可选项
spring.thymeleaf.prefix=classpath:/templates/
#thymeleaf 模版后缀,默认值,可选项
spring.thymeleaf.suffix=.html
package com.songzihao.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}


2.2 遍历List

model 里面的User类、核心配置文件、项目启动入口类和上面的例子是一样的 ,这里就不再给出代码了。

下面直接给出控制层中对应的方法和对应的html页面。

package com.songzihao.springboot.controller;

import com.songzihao.springboot.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
public class UserController {

    @RequestMapping(value = "/each/list")
    public String userList(Model model) {
        List<User> userList=new ArrayList<>();
        for (int i=0;i<10;i++) {
            User user=new User();
            user.setId(1000 + i);
            user.setName("宋小" + i);
            user.setPhone("1364388173" + i);
            user.setAddress("郑州市金水区XX路" + i + "号");
            userList.add(user);
        }
        model.addAttribute("userList",userList);
        return "eachList";
    }

}

th:each="user, userStat : ${userList}" 中的 ${userList} 是后台传过来的集合

◼ user:定义变量,去接收遍历${userList}集合中的一个数据
◼ userStat:${userList} 循环体的信息
◼ 其中 user 及 userStat 自己可以随便取名
◼ userStat 是循环体的信息,通过该变量可以获取如下信息

index: 当前迭代对象的 index (从 0 开始计算)

count: 当前迭代对象的个数(从 1 开始计算) 这两个用的较多
size: 被迭代对象的大小
current: 当前迭代变量
even/odd: 布尔值,当前循环是否是偶数/奇数(从 0 开始计算)
first: 布尔值,当前循环是否是第一个
last: 布尔值,当前循环是否是最后一个
息 注意:循环体信息 userStat 也可以不定义,则默认采用迭代变量加上 Stat 后缀,即 userStat

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>循环遍历List集合</title>
</head>
<body>
    <!--
        user:定义变量,去接收遍历${userList}集合中的每一个数据
        userStat:当前循环的对象状态的变量(可以不写), ${userList} 循环体的信息
        userList:当前循环的集合
    -->
    <div th:each="user,userStat:${userList}">
        <span th:text="${userStat.index}"></span>
        <span th:text="${user.id}"></span>
        <span th:text="${user.name}"></span>
        <span th:text="${user.phone}"></span>
        <span th:text="${user.address}"></span>
    </div>
</body>
</html>


2.3 遍历Map

model 里面的User类、核心配置文件、项目启动入口类和上面的例子是一样的 ,这里就不再给出代码了。

下面直接给出控制层中对应的方法和对应的html页面。

package com.songzihao.springboot.controller;

import com.songzihao.springboot.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
public class UserController {

    @RequestMapping(value = "/each/map")
    public String eachMap(Model model) {
        Map<Integer,Object> userMaps = new HashMap<Integer, Object>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(1000 + i);
            user.setName("宋小" + i);
            user.setPhone("1310179264" + i);
            user.setAddress("洛阳市涧西区XX路" + i + "号");
            userMaps.put(i,user);
        }
        model.addAttribute("userMaps",userMaps);
        return "eachMap";
    }

}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>循环遍历Map集合</title>
</head>
<body>
    <div th:each="user,userStat:${userMaps}">
        <span th:text="${userStat.count}"></span>
        <span th:text="${user.key}"></span>
        <span th:text="${user.value}"></span>
        <span th:text="${user.value.id}"></span>
        <span th:text="${user.value.name}"></span>
        <span th:text="${user.value.phone}"></span>
        <span th:text="${user.value.address}"></span>
    </div>
</body>
</html>

最后说一下,可能这三个例子中遍历出来的结果,不太好看😂😂😂,但是这里纯粹是为了学习Thymeleaf这个模板引擎中的内容,所以没有过多的考虑这个问题,希望大家谅解!!!

  • 13
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

★★光亭★★

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值