【问题标题】:How to iterate through multiple Lists using JSTL foreach loop如何使用 JSTL foreach 循环遍历多个列表
【发布时间】:2015-04-13 03:32:30
【问题描述】:

我有这段代码可以发送两个列表进行迭代

 request.setAttribute("PopulateAgentList", agentList);
 request.setAttribute("PopulateAgentContactList",agentContactList);

使用这段代码我只能遍历一个列表。

<c:forEach var="PopulateAgentList" items="${requestScope['PopulateAgentList']}">
        <tr>
            <td><c:out value="${PopulateAgentList.name}"/></td>
            <td><c:out value="${PopulateAgentList.country}"/></td>
            <td>Edinburgh</td>
            <td>61</td>
        </tr>
        </c:forEach>

我可以遍历“PopulateAgentContactList”和“PopulateAgentList”吗?

【问题讨论】:

  • 这只是说明了一个设计问题。您应该有一个单独的列表,而不是两个并行列表,其中列表中的每个对象都将包含一个代理和一个联系人。

标签: jsp servlets jstl


【解决方案1】:

是的,this answer here explains a way to do this

在你的情况下,你会这样做:

<c:forEach var="PopulateAgentList" items="${people.firstnames}" varStatus="status">
    <tr>
        <td><c:out value="${PopulateAgentList.name}"/></td>
        <td><c:out value="${PopulateAgentList.country}"/></td>
        <td><c:out value="${PopulateAgentContactList[status.index].whatever}"/></td>
    </tr>
</c:forEach>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    相关资源
    最近更新 更多