欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

thymeleaf 遍历

程序员文章站 2022-04-15 19:05:01
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

                        thymeleaf 遍历

                       thymeleaf 遍历

 

 

本文地址:https://blog.csdn.net/weixin_43931625/article/details/107890194

相关标签: thymeleaf