thymeleaf 遍历

thymeleaf 遍历

 

th:each:遍历实现了接口iterable、iterator、enumeration、map的实例对象数组

 

遍历状态变量:自定义

  <tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
    <td th:text="${prod.name}">Onions</td>
    <td th:text="${prod.price}">2.41</td>
    <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
  </tr>

 

遍历状态变量:不设置状态名,则自动添加


  <tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'">
    <td th:text="${prod.name}">Onions</td>
    <td th:text="${prod.price}">2.41</td>
    <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
  </tr>

默认设置为:变量名 + 后缀(Stat)

 

遍历状态变量属性

index:当前索引值,从0开始

count:计数器,从1开始

size:遍历变量总大小

current:当前对象

even | odd:索引奇偶(true | false)

first:当前对象是否是第一个(true|false)

last:当前对象是否是最后一个(true | false)

 

 

**********************

示例

 

***************

pojo 层

 

Person

@Data
public class Person {

    private Integer id;
    private String name;
    private Integer age;
}

 

***************

controller 层

 

HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello(ModelAndView mv){
        List<Person> list=new ArrayList<>();
        for (int i=0;i<5;i++){
            Person person=new Person();
            person.setId(i);
            person.setName("瓜田李下 "+i);
            person.setAge(20+i);

            list.add(person);
        }
        mv.addObject("list",list);

        Person[] array=list.toArray(new Person[]{});
        mv.addObject("array",array);

        Set<Person> set=new HashSet<>();
        for (int i=0;i<5;i++){
            Person person=new Person();
            person.setId(i);
            person.setName("海贼王 "+i);
            person.setAge(20+i);

            set.add(person);
        }
        mv.addObject("set",set);

        Map<Integer,Person> map=new HashMap<>();
        for (int i=0;i<5;i++){
            Person person=new Person();
            person.setId(i);
            person.setName("火影忍者 "+i);
            person.setAge(20+i);

            map.put(i,person);
        }
        mv.addObject("map",map);

        mv.setViewName("test");
        return mv;
    }
}

 

***************

前端页面

 

test.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:align="center" style="color: coral">
    <strong th:align="center" style="color: purple">list 遍历</strong>
    <div th:each="person:${list}">
        <strong>
            <span th:text="${person.id}"></span> ==>
            <span th:text="${person.name}"></span> ==>
            <span th:text="${person.age}"></span><br>
            index:<span th:text="${personStat.index}"></span> ==>
            count:<span th:text="${personStat.count}"></span> ==>
            size:<span th:text="${personStat.size}"></span><br>
            current:<span th:text="${personStat.current}"></span><br>

            even:<span th:text="${personStat.even}"></span> ==>
            odd:<span th:text="${personStat.odd}"></span> ==>
            first:<span th:text="${personStat.first}"></span> ==>
            last:<span th:text="${personStat.last}"></span><br><br>
        </strong>
    </div><br>

    <strong style="color: purple">array 遍历</strong>
    <div th:each="person:${array}">
        <strong>
            <span th:text="${person.id}"></span> ==>
            <span th:text="${person.name}"></span> ==>
            <span th:text="${person.age}"></span>
        </strong>
    </div><br>

    <strong style="color: purple">set 遍历</strong>
    <div th:each="person:${set}">
        <strong>
            <span th:text="${person.id}"></span> ==>
            <span th:text="${person.name}"></span> ==>
            <span th:text="${person.age}"></span>
        </strong>
    </div><br>

    <strong style="color: purple">map 遍历</strong>
    <div th:each="entry:${map}">
        <strong>
            <span th:text="${entry.getKey()}" style="color: hotpink"></span>:
            <span th:text="${entry.getValue().id}"></span> ==>
            <span th:text="${entry.getValue().name}"></span> ==>
            <span th:text="${entry.getValue().age}"></span>
        </strong>
    </div>
</div>
</body>
</html>

 

 

**********************

使用测试

 

localhost:8080/hello

                        

                       

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值