亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Thymeleaf的三種循環(huán)遍歷方式

 更新時(shí)間:2022年06月30日 10:22:42   作者:小威要向諸佬學(xué)習(xí)呀  
Thymeleaf?是一款用于渲染?XML/XHTML/HTML5?內(nèi)容的模板引擎。本文為大家總結(jié)了Thymeleaf的三種循環(huán)遍歷方式,感興趣的可以跟隨小編一起學(xué)習(xí)一下

循環(huán)遍歷list集合

1.實(shí)體類

使用lombok插件,省去getter和setter,toString等方法的書(shū)寫

代碼

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等注解

代碼

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

代碼

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>循環(huán)遍歷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>

說(shuō)明

1.user指的是當(dāng)前循環(huán)的對(duì)象的變量名稱,可以隨意定義,但要于下面 " . 屬性"引用保持一致相當(dāng)于增強(qiáng)for循環(huán)的臨時(shí)變量

2.userStat指當(dāng)前循環(huán)對(duì)象狀態(tài)的變量(可選,默認(rèn)就是你第一步設(shè)置的對(duì)象變量名稱+ Stat)

3.${userList }是當(dāng)前循環(huán)的集合

其中userStat有很多屬性

他們的結(jié)果按順序展示如下

current展示當(dāng)前的user對(duì)象 index是索引屬性,從0開(kāi)始 count是計(jì)數(shù),下標(biāo)從1開(kāi)始 first,last,odd,even均是返回boolean值,分別判斷下標(biāo)是否為第一個(gè)/最后一個(gè)/奇數(shù)/偶數(shù) size指的是當(dāng)前userList的大小,返回的是同一個(gè)值

循環(huán)遍歷map集合

1.控制類

代碼

 @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

代碼

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>循環(huán)遍歷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遍歷結(jié)果

map集合和list集合遍歷類似

循環(huán)遍歷數(shù)組

數(shù)組的遍歷和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="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>循環(huán)遍歷數(shù)組</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>

遍歷結(jié)果

到此這篇關(guān)于詳解Thymeleaf的三種循環(huán)遍歷方式的文章就介紹到這了,更多相關(guān)Thymeleaf循環(huán)遍歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringCloud Feign遠(yuǎn)程調(diào)用與自定義配置詳解

    SpringCloud Feign遠(yuǎn)程調(diào)用與自定義配置詳解

    Feign是Netflix公司開(kāi)發(fā)的一個(gè)聲明式的REST調(diào)用客戶端; Ribbon負(fù)載均衡、 Hystrⅸ服務(wù)熔斷是我們Spring Cloud中進(jìn)行微服務(wù)開(kāi)發(fā)非?;A(chǔ)的組件,在使用的過(guò)程中我們也發(fā)現(xiàn)它們一般都是同時(shí)出現(xiàn)的,而且配置也都非常相似
    2022-11-11
  • IDEA內(nèi)存調(diào)試插件(好用)

    IDEA內(nèi)存調(diào)試插件(好用)

    本文給大家分享IDEA中一個(gè)很有用的內(nèi)存調(diào)試插件,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下
    2018-02-02
  • intellij idea自動(dòng)生成類注釋和方法注釋配置方法

    intellij idea自動(dòng)生成類注釋和方法注釋配置方法

    這篇文章主要介紹了intellij idea自動(dòng)生成類注釋和方法注釋設(shè)置方法,需要的朋友可以參考下
    2023-01-01
  • java中的arrays.sort()代碼詳解

    java中的arrays.sort()代碼詳解

    這篇文章主要介紹了Java中的Arrays.sort()代碼詳解,涉及Arrays.sort()簡(jiǎn)單示例,策略模式,”super”的使用等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Java如何分析算法的時(shí)間和空間復(fù)雜度

    Java如何分析算法的時(shí)間和空間復(fù)雜度

    這篇文章主要介紹了Java如何分析算法的時(shí)間和空間復(fù)雜度,在計(jì)算機(jī)科學(xué)中,計(jì)算復(fù)雜性解釋了算法的性能。文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-06-06
  • SpringBoot 并發(fā)登錄人數(shù)控制的實(shí)現(xiàn)方法

    SpringBoot 并發(fā)登錄人數(shù)控制的實(shí)現(xiàn)方法

    這篇文章主要介紹了SpringBoot 并發(fā)登錄人數(shù)控制的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Spring單元測(cè)試類ApplicationTests錯(cuò)誤的解決

    Spring單元測(cè)試類ApplicationTests錯(cuò)誤的解決

    這篇文章主要介紹了Spring單元測(cè)試類ApplicationTests錯(cuò)誤的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java 對(duì) Properties 文件的操作詳解及簡(jiǎn)單實(shí)例

    Java 對(duì) Properties 文件的操作詳解及簡(jiǎn)單實(shí)例

    這篇文章主要介紹了Java 對(duì) Properties 文件的操作詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • SpringBoot配置Clickhouse的示例代碼

    SpringBoot配置Clickhouse的示例代碼

    這篇文章主要介紹了SpringBoot配置Clickhouse的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考價(jià)值,需要的朋友可以參考下
    2022-02-02
  • 聊聊SpringBoot的@Scheduled的并發(fā)問(wèn)題

    聊聊SpringBoot的@Scheduled的并發(fā)問(wèn)題

    這篇文章主要介紹了聊聊SpringBoot的@Scheduled的并發(fā)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評(píng)論