Thymeleaf的循环遍历方式有哪些(thymeleaf,开发技术)

时间:2024-05-02 14:19:17 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

接下来,请跟着小编一起来学习吧!

    循环遍历list集合

    1.实体类

    使用lombok插件,省去getter和setter,toString等方法的书写

    Thymeleaf的循环遍历方式有哪些

    代码

    packagecom.springboot_thyleaf2.model;

    importlombok.Data;

    @Data
    publicclassUser{
    privateIntegerid;
    privateStringnick;
    privateStringphone;
    privateStringaddress;
    }

    2.控制类

    使用controller等注解

    Thymeleaf的循环遍历方式有哪些

    代码

    importjava.util.ArrayList;
    importjava.util.List;

    @Controller
    publicclassUserController{
    @RequestMapping("/each/list")
    publicStringeachList(Modelmodel){
    List<User>userList=newArrayList<>();
    for(inti=0;i<10;i++){
    Useruser=newUser();
    user.setId(100+i);
    user.setNick("陈"+i);
    user.setPhone("123456"+i);
    user.setAddress("苏杭"+i);
    userList.add(user);
    }
    model.addAttribute("userList",userList);
    return"eachList";
    }
    }

    3.each.html

    Thymeleaf的循环遍历方式有哪些

    代码

    <!DOCTYPEhtml>
    <htmllang="en"xmlns:th="http://www.thymeleaf.org&quot;&gt;
    <head>
    <metacharset="UTF-8">
    <title>循环遍历list集合</title>
    </head>
    <body>
    <divth:each="user,userStat:${userList}">
    <spanth:text="${userStat.current}"/>
    <spanth:text="${user.id}"/>
    <spanth:text="${user.nick}"/>
    <spanth:text="${user.phone}"/>
    <spanth:text="${user.address}"/>
    </div>
    </body>
    </html>

    说明

    1.user指的是当前循环的对象的变量名称,可以随意定义,但要于下面 " . 属性"引用保持一致相当于增强for循环的临时变量

    2.userStat指当前循环对象状态的变量(可选,默认就是你第一步设置的对象变量名称+ Stat)

    3.${userList }是当前循环的集合

    其中userStat有很多属性

    Thymeleaf的循环遍历方式有哪些

    他们的结果按顺序展示如下

    Thymeleaf的循环遍历方式有哪些

    current展示当前的user对象 index是索引属性,从0开始 count是计数,下标从1开始 first,last,odd,even均是返回boolean值,分别判断下标是否为第一个/最后一个/奇数/偶数 size指的是当前userList的大小,返回的是同一个值

    循环遍历map集合

    1.控制类

    Thymeleaf的循环遍历方式有哪些

    代码

    @RequestMapping("/each/map")
    publicStringeachMap(Modelmodel){
    Map<Integer,Object>userMaps=newHashMap<>();
    for(inti=0;i<10;i++){
    Useruser=newUser();
    user.setId(i);
    user.setNick("王"+i);
    user.setPhone("123456"+i);
    user.setAddress("苏杭"+i);
    userMaps.put(i,user);
    }
    model.addAttribute("userMaps",userMaps);
    return"eachMap";
    }
    }

    2.each.html

    Thymeleaf的循环遍历方式有哪些

    代码

    <!DOCTYPEhtml>
    <htmllang="en"xmlns:th="http://www.thymeleaf.org&quot;&gt;
    <head>
    <metacharset="UTF-8">
    <title>循环遍历Map集合</title>
    </head>
    <body>
    <divth:each="userMap,userMapStat:${userMaps}">
    <spanth:text="${userMapStat.index}"/>
    <spanth:text="${userMapStat.count}"/>
    <spanth:text="${userMap.getKey()}"/>
    <spanth:text="${userMap.value}"/>
    <spanth:text="${userMap.value.id}"/>
    <spanth:text="${userMap.value.nick}"/>
    <spanth:text="${userMap.value.phone}"/>
    <spanth:text="${userMap.value.address}"/>

    </div>
    </body>
    </html>

    map遍历结果

    Thymeleaf的循环遍历方式有哪些

    map集合和list集合遍历类似

    循环遍历数组

    数组的遍历和list的遍历一样,看到这里可以不用看了。

    控制类代码

    @RequestMapping("/each/array")
    publicStringeachArray(Modelmodel){
    User[]userArray=newUser[10];
    for(inti=0;i<10;i++){
    Useruser=newUser();
    user.setId(i);
    user.setNick("李"+i);
    user.setPhone("123456"+i);
    user.setAddress("苏杭"+i);
    userArray[i]=user;
    }
    model.addAttribute("userArray",userArray);
    return"eachArray";
    }
    }

    eachArray.html

    <!DOCTYPEhtml>
    <htmllang="en"xmlns:th="http://www.thymeleaf.org&quot;&gt;
    <head>
    <metacharset="UTF-8">
    <title>循环遍历数组</title>
    </head>
    <body>
    <divth:each="user,userStat:${userArray}">
    <spanth:text="${userStat.index}"/>
    <spanth:text="${userStat.count}"/>
    <spanth:text="${user.id}"/>
    <spanth:text="${user.nick}"/>
    <spanth:text="${user.phone}"/>
    <spanth:text="${user.address}"/>
    </div>
    </body>
    </html>

    遍历结果

    Thymeleaf的循环遍历方式有哪些

    若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

    本文:Thymeleaf的循环遍历方式有哪些的详细内容,希望对您有所帮助,信息来源于网络。
    上一篇:Vue的双端diff算法怎么实现下一篇:

    4 人围观 / 0 条评论 ↓快速评论↓

    (必须)

    (必须,保密)

    阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18