c/c++语言开发共享详解Thymeleaf的三种循环遍历方式

循环遍历list集合1.实体类使用lombok插件,省去getter和setter,tostring等方法的书写代码package com.springboot_thyleaf2.model;impo

循环遍历list集合

1.实体类

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

详解Thymeleaf的三种循环遍历方式

代码

package com.springboot_thyleaf2.model;    import lombok.data;    @data  public class user {      private integer id;      private string nick;      private string phone;      private string address;  }    

2.控制类

使用controller等注解

详解Thymeleaf的三种循环遍历方式

代码

import java.util.arraylist;  import java.util.list;    @controller  public class usercontroller {      @requestmapping("/each/list")      public string eachlist(model model){          list<user> userlist=new arraylist<>();          for (int i=0;i<10;i++){              user user=new user();              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的三种循环遍历方式

代码

<!doctype html>  <html lang="en" xmlns:th="https://www.thymeleaf.org" >  <head>      <meta charset="utf-8">      <title>循环遍历list集合</title>  </head>  <body>  <div th:each="user,userstat:${userlist}">      <span th:text="${userstat.current}"/>      <span th:text="${user.id}"/>      <span th:text="${user.nick}"/>      <span th:text="${user.phone}"/>      <span th: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")      public string eachmap(model model){          map<integer,object> usermaps=new hashmap<>();          for(int i=0;i<10;i++){              user user=new user();              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的三种循环遍历方式

代码

<!doctype html>  <html lang="en" xmlns:th="https://www.thymeleaf.org" >  <head>      <meta charset="utf-8">      <title>循环遍历map集合</title>  </head>  <body>  <div th:each="usermap,usermapstat:${usermaps}">      <span th:text="${usermapstat.index}"/>      <span th:text="${usermapstat.count}"/>      <span th:text="${usermap.getkey()}"/>      <span th:text="${usermap.value}"/>      <span th:text="${usermap.value.id}"/>      <span th:text="${usermap.value.nick}"/>      <span th:text="${usermap.value.phone}"/>      <span th:text="${usermap.value.address}"/>    </div>  </body>  </html>  

map遍历结果

详解Thymeleaf的三种循环遍历方式

map集合和list集合遍历类似

循环遍历数组

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

控制类代码

    @requestmapping("/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.setnick("李"+i);              user.setphone("123456"+i);              user.setaddress("苏杭"+i);              userarray[i]=user;          }          model.addattribute("userarray",userarray);          return "eacharray";      }  }  

eacharray.html

<!doctype html>  <html lang="en" xmlns:th="https://www.thymeleaf.org" >  <head>      <meta charset="utf-8">      <title>循环遍历数组</title>  </head>  <body>  <div th:each="user,userstat:${userarray}">      <span th:text="${userstat.index}"/>      <span th:text="${userstat.count}"/>      <span th:text="${user.id}"/>      <span th:text="${user.nick}"/>      <span th:text="${user.phone}"/>      <span th:text="${user.address}"/>  </div>  </body>  </html>  

遍历结果

详解Thymeleaf的三种循环遍历方式

到此这篇关于详解thymeleaf的三种循环遍历方式的文章就介绍到这了,更多相关thymeleaf循环遍历内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享详解Thymeleaf的三种循环遍历方式,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1237358.html

(0)
上一篇 2022年9月7日
下一篇 2022年9月7日

精彩推荐