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

时间:2018-10-12 05:59:19

标签: java spring jsp jstl

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

enter image description here

如上图所示, 记录第一 100应该与“支付代码”列对应 基本付款应与说明对应 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>

预期输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

您应将列表项迭代为表行(<tr>),对于每一行,将列表项中的映射迭代为表单元格(<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个项目,则可以使用状态var和索引值对其进行过滤:

<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>