java - 使用 JSTL 从 Java Bean 迭代 ArrayList<Map<String, String>>?

标签 java dictionary arraylist jstl

解决这个问题有点困难。在我的 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 Map 的 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 个表格行。

非常感谢任何帮助。

最佳答案

我建议使用对象来存储所有值,而不是 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>

希望有帮助。

关于java - 使用 JSTL 从 Java Bean 迭代 ArrayList<Map<String, String>>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36991628/

相关文章:

java - 将资源加载到基于图 block 的游戏的 ArrayList 中。 ( java )

java - 按进程划分的浏览器关闭事件

Java - 使用 PreparedStatement 在 mysql 数据库中存储 UTF-8 字符(特别是俄语字符)

java - 代码中最好的 "identifying markers"有助于快速导航到代码中的某个位置?

c# - 使用 LINQ 从 dictionary<string,List<string>> 中删除重复值

python - 有没有一种简单的方法可以在 Python 中同时使用 "append()"两个字典?

python - 如何通过单项从字典中获取单值?

java - 为什么处理排序数组比未排序数组*慢*? (Java 的 ArrayList.indexOf)

java - 数组列表未按预期工作

java - 带系统 : A resource failed to call release