詳解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)用與自定義配置詳解
Feign是Netflix公司開發(fā)的一個聲明式的REST調(diào)用客戶端; Ribbon負載均衡、 Hystrⅸ服務熔斷是我們Spring Cloud中進行微服務開發(fā)非常基礎的組件,在使用的過程中我們也發(fā)現(xiàn)它們一般都是同時出現(xiàn)的,而且配置也都非常相似2022-11-11
SpringBoot 并發(fā)登錄人數(shù)控制的實現(xiàn)方法
這篇文章主要介紹了SpringBoot 并發(fā)登錄人數(shù)控制的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05
Spring單元測試類ApplicationTests錯誤的解決
這篇文章主要介紹了Spring單元測試類ApplicationTests錯誤的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Java 對 Properties 文件的操作詳解及簡單實例
這篇文章主要介紹了Java 對 Properties 文件的操作詳解及簡單實例的相關資料,需要的朋友可以參考下2017-02-02
聊聊SpringBoot的@Scheduled的并發(fā)問題
這篇文章主要介紹了聊聊SpringBoot的@Scheduled的并發(fā)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

