欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

1.實體類

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

代碼

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>

說明

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

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

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

其中userStat有很多屬性

他們的結果按順序展示如下

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

循環(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遍歷結果

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>

遍歷結果

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

相關文章

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

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

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

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

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

    intellij idea自動生成類注釋和方法注釋配置方法

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

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

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

    Java如何分析算法的時間和空間復雜度

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

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

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

    Spring單元測試類ApplicationTests錯誤的解決

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

    Java 對 Properties 文件的操作詳解及簡單實例

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

    SpringBoot配置Clickhouse的示例代碼

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

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

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

最新評論