【问题标题】:Iterate over an ArrayList< Map<String, String>> from a Java Bean using JSTL?使用 JSTL 从 Java Bean 迭代 ArrayList< Map<String, String>>?
【发布时间】:2016-05-02 21:01:02
【问题描述】:

对此进行排序有点困难。在我的 JSP 页面中,我有一个表格,其中显示了 x 行数和 3 列。在我的 Java bean 中,我将其声明为:

private ArrayList<Map<String, String>> issueClient = new ArrayList<>();
private ArrayList<Map<String, String>> issueR2 = new ArrayList<>();

我通过 MVC 获取我的值并将它们放入 Map 中:

 if (crs != null && crs.size() > 0) {
        try {
            while (crs.next()) {
                HashMap<String, String> tmpPart = new HashMap<>();

                tmpPart.put("part", crs.getString("part"));
                tmpPart.put("type", crs.getString("part_type"));
                tmpPart.put("id_issue", crs.getString("id_part"));
                tmpPart.put("issue", crs.getString("part_name"));

                repairPartAddNew.addIssueClientMap(tmpPart);
            }
        } catch (Exception e) {
         ....
        }
    } else {
      ....
    }

repairPartAddNew 是对包含上述 ArrayList 映射的 Java Bean 的引用。在我的 JSP 中,我已经开始构建以下内容:

<c:if test="${repairPartAddNew.issueClient != null}">
<c:set var="loopValue" value="1" scope="page" />
   <c:forEach var="issueClient" items="${repairPartAddNew.issueClient}" varStatus="loop">
      <c:forEach var="map" items="${issueClient}">
        <tr>
          <td>
            <form:select disabled="disabled" multiple="false" id="clientReportIssuePart${loopValue}" class="form-control input-sm" name="clientReportIssuePart" path="clientReportIssuePart" onchange="getPartSubtypes(this.value,$(this).parent().next().find('select').attr('id'));">
            <form:option value="${map.value}">${map.value}</form:option>
            <form:options items="${descriptionParts}" itemValue="value" itemLabel="name"/>
             </form:select>
           </td>
           <td>
              <form:select disabled="disabled" multiple="false" class="form-control input-sm" id="clientPartType${loopValue}" name="clientPartType" path="clientPartType" onchange="getRepairPartIssue(this.value,$(this).parent().next().find('select').attr('id'),$(this).parent().prev().find('select').val());">
              <form:option value="${map.value}">${map.value}</form:option>
              </form:select>
           </td>
           <td>
             <form:select disabled="disabled" multiple="false" class="form-control input-sm" id="clientRepair${loopValue}" name="clientReportIssuePartId" path="clientReportIssuePartId">
             </form:select>
           </td>
         </tr>
   </c:forEach>
 <c:set var="loopValue" value="${loopValue + 1}" scope="page"/>
</c:forEach>
</c:if>

我最终得到了数据结构中的各种可憎,包括http://prnt.sc/azjlgp

似乎遍历 Array 和 Map 会产生重复的行。在此示例中,应该只有 2 个表格行。

非常感谢任何帮助。

【问题讨论】:

  • 如果列表中的每个地图都应该是一行,为什么你有两个嵌套的forEach?你到底为什么要用地图来表示你的模型,而不是对象,用命名的、类型化的属性?
  • @JBNizet 你说得对,我没有从正确的角度看它。

标签: java dictionary arraylist jstl


【解决方案1】:

我建议使用对象而不是 HashMap 来存储您的所有值。然后,您可以将每一行数据存储在该对象中,并将它们全部存储在 ArrayList 中。然后,您应该能够根据需要从对象中提取每个值。

        <c:if test="${repairPartAddNew.clientReportIssuePart != null}">
                <c:set var="loopValue" value="1" scope="page" />
                <c:forEach var="issueClient" items="${repairPartAddNew.issueClientList}" varStatus="loop">
                        <tr>
                            <td>
                                <form:select disabled="disabled" multiple="false" id="clientReportIssuePart${loopValue}" class="form-control input-sm" name="clientReportIssuePart" path="clientReportIssuePart"
                                             onchange="getPartSubtypes(this.value,$(this).parent().next().find('select').attr('id'));">
                                    <form:option value="${issueClient.part}">${issueClient.part}</form:option>
                                    <form:options items="${descriptionParts}" itemValue="value" itemLabel="name"/>
                                </form:select>
                            </td>

                            <td>
                                <form:select disabled="disabled" multiple="false" class="form-control input-sm" id="clientPartType${loopValue}" name="clientPartType" path="clientPartType"
                                             onchange="getRepairPartIssue(this.value,$(this).parent().next().find('select').attr('id'),$(this).parent().prev().find('select').val());">
                                    <form:option value="${issueClient.type}">${issueClient.type}</form:option>
                                </form:select>
                            </td>

                            <td>
                                <form:select disabled="disabled" multiple="false" class="form-control input-sm" id="clientRepair${loopValue}" name="clientReportIssuePartId" path="clientReportIssuePartId">
                                    <form:option value="${issueClient.id_issue}">${issueClient.issue}</form:option>
                                </form:select>
                            </td>
                            <td class="text-center width-100">
                                <button type="button" id="deleteClientRowBtn" class="btn btn-outline btn-sm btn-dark"
                                        onclick="deleteRow(this, $(this).closest('table').attr('id'))">Delete Row
                                </button>
                            <td class="text-center width-100">
                                <button type="button" class="btn btn-outline btn-sm btn-dark"
                                        onclick="addTableRow('tableClientTemplate', $(this).closest('table').attr('id'))">Add Row
                                </button>
                            </td>
                        </tr>
                    <c:set var="loopValue" value="${loopValue + 1}" scope="page"/>
                </c:forEach>
            </c:if>

        </tbody>

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2014-10-19
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 2018-08-02
    • 2015-12-05
    • 2011-12-02
    相关资源
    最近更新 更多