TheShadow123
TheShadow123

Reputation: 185

How to iterate through multiple Lists using JSTL foreach loop

I have this code to send two Lists for iterating

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

I can iterate through only one List usijng this code.

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

Can I iterate through the both "PopulateAgentContactList" and "PopulateAgentList".

Upvotes: 1

Views: 8178

Answers (1)

chairbender
chairbender

Reputation: 849

Yes, this answer here explains a way to do this

In your situation, you would do something like 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>

Upvotes: 3

Related Questions