迭代 Map Inside List 以便将值显示为 JSP 中的行

Iterate Map Inside List in order to display the values as row in JSP

我需要显示Map内的值,而Map在List内。 现在,我的值正在显示,但以列的方式显示(如截图所示)。不是以行方式,我想以列方式显示所有值

如上图所示, 拿第一条记录, 100 应该映射到 Pay Code 列 BASIC PAY 应与 Description 对应 25300 应该映射到 Amount 和2,没用,我不想再显示了。

我的获取数据的服务方法:

public List<Map<String, Object>> searchPayCodeByempCode(String tabSuffix, String empCode, String yyyyMm) {
    MapSqlParameterSource param = new MapSqlParameterSource();
    String tableName = "Salary_detail_report_082018";
    String query = "SELECT "
            + " DISTINCT PAY_CODE, "
            + " PAY_CODE_DESC, "
            + " AMOUNT, "
            + " row_number() over (Order by EMP_CODE ) AS ROW_NUM "
            + " FROM " + tableName
            + " WHERE EMP_CODE=" + empCode
            + " AND YYYYMM=" + yyyyMm
            + " AND PAY_CODE NOT IN (997,998,999) "
            + " ORDER BY PAY_CODE ASC ";
    List<Map<String, Object>> employees = queryForList(query);
    if (employees != null && !employees.isEmpty()) {
        for (Map<String, Object> employee : employees) {
             System.out.println("The set is: " + employee.keySet()); 
            for (Iterator<Map.Entry<String, Object>> it = employee.entrySet().iterator(); it.hasNext();) {
                Map.Entry<String, Object> entry = it.next();
                String key = entry.getKey();
                Object value = entry.getValue();
                System.out.println(key + " = " + value);
            }
        }
    }
    return employees;

JSP代码:

 <table class="table table-striped table-bordered dataTable no-footer tableWidth"  role="grid" name="employeeList" id="employeeList">
                                <thead>
                                    <tr>
                                        <th width="5%">S. No.</th>
                                        <th width="10%">Pay Code</th>
                                        <th width="15%">Description</th>
                                        <th width="10%">Amount</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <c:forEach var="map" items="${mapList}">
                                        <c:forEach var="mapEntry" items="${map}">
                                            <tr>
                                                <%--<td>test : ${mapEntry.key}</td>--%>
                                                <td>test : ${mapEntry.value}</td>
                                            </tr>
                                        </c:forEach>
                                    </c:forEach>
                                </tbody>
                            </table>

预期输出:

您应该将列表项迭代为 table 行 (<tr>),并将列表项中的每一行映射迭代为 table 单元格 (<td>):

<tbody>
    <c:forEach var="map" items="${mapList}">
        <tr>
            <c:forEach var="mapEntry" items="${map}">
                <td>${mapEntry.value}</td>
            </c:forEach>
        </tr>
    </c:forEach>
</tbody>

区别在<tr>位置。

如果您在地图中有更多项目并且只想渲染前 4 个,您可以使用状态变量和索引值来过滤它们:

<c:forEach var="mapEntry" items="${map}" varStatus="status">
    <c:if test="${status.index <= 4 }">
        <td>${mapEntry.value}</td>
    </c:if>
</c:forEach>

但是如果您的地图顺序不正确(例如使用 HashMap 时),那么最好有选择地渲染单元格:

<tbody>
    <c:forEach var="map" items="${mapList}">
        <tr>
            <td width="5%">${map.get("ROW_NUM")}</td>
            <td width="10%">${map.get("PAY_CODE")}</td>
            <td width="15%">${map.get("PAY_CODE_DESC")}</td>
            <td width="10%">${map.get("AMOUNT")}</td>
        </tr>
    </c:forEach>
</tbody>