springboot thymeleaf遍历List<Map>

一、问题描述

现有如下数据,结构是List<Map<String,Object>>,并且Map中还嵌套了Map


需要在页面上显示出来,效果如下:


二、代码实现

StudentController.java

@Controller
@RequestMapping("studentMgmt")
public class StudentController {
    @RequestMapping("queryStudentInfo")
    public String queryStudentInfo(Model model) {
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        Map<String, Object> student = new HashMap<String, Object>(){{
            put("sid", "101");
            put("sname", "张三");
            put("sage", "20");
            put("scourse", new HashMap<String, String>(){{
                put("cname", "语文,数学,英语");
                put("cscore", "93,95,98");
            }});
        }};
        resultList.add(student);
        student = new HashMap<String, Object>(){{
            put("sid", "102");
            put("sname", "李四");
            put("sage", "30");
            put("scourse", new HashMap<String, String>(){{
                put("cname", "物理,化学,生物");
                put("cscore", "92,93,97");
            }});
        }};
        resultList.add(student);
        model.addAttribute("resultList", resultList);
        return "student/studentInfo";
    }
}

studentInfo.html(完整路径 /resources/templates/student/studentInfo.html)

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>学生信息</title>
    <style type="text/css">
        table {
            border: 1px solid black;
            text-align: center;
            border-collapse: collapse;
        }
        table thead th {
            border: 1px solid black;
        }
        table tbody td {
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <thead>
            <th>序号</th>
            <th>学生id</th>
            <th>学生姓名</th>
            <th>学生年龄</th>
            <th>学生课程-课程列表</th>
            <th>学生课程-课程分数</th>
        </thead>
        <tbody>
            <tr th:each="entries,stat:${resultList}" th:style="'background-color:'+@{${stat.odd}?'#F2F2F2'}">
                <td th:text="${stat.count}"></td>
                <td th:text="${entries['sid']}"></td>
                <td th:text="${entries['sname']}"></td>
                <td th:text="${entries['sage']}"></td>
                <!-- 写法一:
                <td th:text="${entries['scourse']['cname']}"></td>
                <td th:text="${entries['scourse']['cscore']}"></td>-->
                <!-- 写法二:
                <td th:text="*{entries['scourse']['cname']}"></td>
                <td th:text="*{entries['scourse']['cscore']}"></td>-->
                <!-- 写法三:-->
                <td th:object="${entries['scourse']}">
                    <span th:text="*{['cname']}"></span>
                </td>
                <td th:object="${entries['scourse']}">
                    <span th:text="*{['cscore']}"></span>
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>

补充:

注意*{}(选择表达式)和${}(变量表达式)的区别:选择表达式计算的是选定的对象(th:object),变量表达式计算的是整个环境变量映射(写法一和写法三)

如果没有选定的对象,选择表达式和变量表达式效果是一样的(写法一和写法二)


参考链接:

How to use nested maps in thymeleaf?

thymeleaf 基本表达式

  • 14
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值