25.在springboot中使用thymeleaf循环(list,array,map)

说明:

本项目是在https://blog.csdn.net/weixin_59334478/article/details/126749179?spm=1001.2014.3001.5501https://blog.csdn.net/weixin_59334478/article/details/126749179?spm=1001.2014.3001.5501

springboot文章23

修改而来

pom.xml文件,核心配置文件等文件,与文章23保持一致,本文只展示最新添加的文件,以减小文章冗余,更容易看出循环的使用方法。

知识点:

语法说明:
th:each="user, iterStat : ${userlist}" 中的 ${userList} 是后台传过来的集合
user
定义变量,去接收遍历 ${userList} 集合中的一个数据
iterStat
${userList} 循环体的信息
其中 user iterStat 自己可以随便取名
interStat 是循环体的信息,通过该变量可以获取如下信息
index: 当前迭代对象的 index(从 0 开始计算)
count: 当前迭代对象的个数(从 1 开始计算)这两个用的较多
size: 被迭代对象的大小
current: 当前迭代变量
even/odd: 布尔值,当前循环是否是偶数 / 奇数(从 0 开始计算)
first: 布尔值,当前循环是否是第一个
last: 布尔值,当前循环是否是最后一个
注意:循环体信息 interStat 也可以不定义,则默认采用迭代变量加上 Stat 后缀,即userStat

1.index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index.html</title>
</head>
<body>
<h3>index.html------学习模板thymeleaf的语法</h3>
<a href="tpl/expression1">标准变量表达式</a>
<br/>
<a href="tpl/expression2">选择变量表达式</a>
<br/>
<a href="tpl/link">链接表达式</a>
<br/>
<a href="tpl/eachList">循环List</a><br/>
<a href="tpl/eachArray">循环Array</a><br/>
<a href="tpl/eachMap">循环Map</a><br/>

</body>
</html>

 2.ThymeleafController类

package com.it.controller;

import com.it.entity.SysUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tpl")
public class ThymeleafController {


    //标准变量表达式
    @GetMapping("/expression1")
    public String expression1(Model model){
        //添加数据到model
        model.addAttribute("site","www.baidu.com");
        model.addAttribute("myuser",new SysUser(1001,"小绿","女",22));

        //指定视图
        return "expression1";
    }

    //选择变量表达式
    @GetMapping("/expression2")
    public String expression2(Model model){
        //添加数据到model
        model.addAttribute("site2","www.bibili.com");
        model.addAttribute("myuser2",new SysUser(1002,"小王","男",26));

        //指定视图
        return "expression2";
    }


    //链接表达式
    @GetMapping("/link")
    public String link(Model model){
        model.addAttribute("userId",1003);
        return "link";
    }

    //测试链接表达式的地址
    @GetMapping("/queryAccount")
    @ResponseBody
    public String queryAccount(Integer id){
        return "queryAccount,参数id="+id;
    }

    //测试链接表达式有两个参数的地址
    @GetMapping("queryUser")
    @ResponseBody
    public String queryUser(String name,Integer age){
        return "queryUser,有两个参数:name="+name+",age="+age;
    }


    //循环list
    @GetMapping("/eachList")
    public String eachList(Model model){
        List<SysUser> list=new ArrayList<>();
        list.add(new SysUser(1002,"张山","男",20));
        list.add(new SysUser(1003,"小明","男",20));
        list.add(new SysUser(1004,"小红","女",22));
        list.add(new SysUser(1005,"小花","女",21));
        model.addAttribute("myusers",list);
        return "eachList";
    }


    //循环数组
    @GetMapping("/eachArray")
    public String eachArray(Model model){
        SysUser userArray[]=new SysUser[3];
        userArray[0]=new SysUser(1001,"小王","男",23);
        userArray[1]=new SysUser(1002,"小李","男",22);
        userArray[2]=new SysUser(1003,"小胡","女",23);
        model.addAttribute("userarray",userArray);
        return "eachArray";
    }

    //循环map
    @GetMapping("/eachMap")
    public String eachMap(Model model){
        Map<String,SysUser> map=new HashMap<>();
        map.put("user1",new SysUser(1001,"小黑","男",23));
        map.put("user2",new SysUser(1002,"小白","男",22));
        map.put("user3",new SysUser(1003,"小红","女",23));
        model.addAttribute("mymap",map);
        return "eachMap";
    }


}

3.eachList.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>each循环</title>
</head>
<body>

<div style="margin-left: 400px">
    <!--<div th:each="user,userI:${myusers}">
        <p th:text="${user.id}"></p>
        <p th:text="${user.name}"></p>
        <p th:text="${user.sex}"></p>
        <p th:text="${user.age}"></p>
    </div>-->
    <br/>

    <table border="1">
        <thead>
        <tr>
            <td>编号</td>
            <td>id序号</td>
            <td>name</td>
            <td>sex</td>
            <td>age</td>
        </tr>
        </thead>
        <tbody>
        <tr th:each="user,userI:${myusers}">
            <td th:text="${userI.count}"></td>
            <td th:text="${user.id}"></td>
            <td th:text="${user.name}"></td>
            <td th:text="${user.sex}"></td>
            <td th:text="${user.age}"></td>
        </tr>
        </tbody>
    </table>

</div>

</body>
</html>

4.eachArray.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>each循环</title>
</head>
<body>

<div style="margin-left: 400px">
   <div th:each="user:${userarray}">
       <p th:text="${user.id}"></p>
       <p th:text="${user.name}"></p>
       <p th:text="${user.sex}"></p>
       <p th:text="${user.age}"></p>
       <br/>
       <hr/>
   </div>

</div>

</body>
</html>

5.eachMap.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>循环map</title>
</head>
<body>
<div style="margin-left: 400px">
    <div th:each="map,mapStat:${mymap}">
        <p th:text="${map.key}"></p>
        <p th:text="${map.value}"></p>
        <p th:text="${map.value.id}"></p>
        <p th:text="${map.value.name}"></p>
        <p th:text="${map.value.sex}"></p>
        <p th:text="${map.value.age}"></p>
        <p th:text="${mapStat.count}"></p>
    </div>
</div>
</body>
</html>

测试:

1.list循环测试

 循环遍历的数据按照原样展示输出

 循环遍历的数据加上循环变量的编号,按照表格的样式进行展示

 2.array循环测试

 

点击循环Array

3.map循环测试

点击循环Map

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
## springboot整合thymeleaf ### 1. 导入起步依赖 ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` ### 2. 更改引入版本 ```xml 3.0.2.RELEASE 2.1.1 ``` > 1. springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。 > 2. 使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。 > 3. 锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。 ### 3. 配置文件配置 ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` > spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html 显示欢迎 显示欢迎 ``` ### 2. 替换属性 ```html 显示欢迎 ``` ### 3. 在表达式访问属性域 ```html 访问属性域 访问请求域 方式一 访问请求域 方式二 访问Session域 访Session域 方式一 访问Application域 方式一 ``` ### 4. 解析url地址 ```html 解析URL地址,获取ContextPath的值 @{}是把ContextPath的值附加到指定的地址前 @{}是把ContextPath的值附加到指定的地址前 ``` ### 5. 直接执行表达式 ```html 直接执行表达式 无转义效果 : [[${attrRequestScope}]] 有转义效果 : [(${attrRequestScope})] ``` ### 6. 分支与迭代 #### 1. if 判断 ```html if判断字符串是否为空 <p th
【资源说明】 1、基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 4、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一道光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值