how to code foreach involving 3 lists in jstl?

2019-08-20 04:05发布

this is a follow on from this question.. how to display 3 arraylist's in a jsp?

well i have this following code in java.. i need an equivalent one for use in jsp.. jstl codes preferred..

 int i=0;
 for(AuctionDo list : auctionDOList)                                   
    {
     System.out.println(" "+ list.getAuctionId()+ " " + depotDolist.get(i).getDepotName() + " " + userAuctionRelDolist2.get(i).getAuctionId() );
     i++;
    }

the three lists are received in jsp.. thankyou.. 1 very important thing i want to display the lists in one row in a table.. so multiple foreach is out of option and tried merging the lists.. pretty much didnt work..

3条回答
Emotional °昔
2楼-- · 2019-08-20 04:33

You have to do like this

<c:forEach items="${list}" var="firstListElement" varStatus="counter"> 
  <%-- auction id of list --%>
  ${list.auctionId}
  <%-- depot name of depotList item based on iteration count --%>
  ${depotList[counter.count].depotName} 
   <%-- auctionId of userAuctionRelDolist2 item based on iteration count --%>
  ${userAuctionRelDolist2[counter.count].auctionId}
</c:forEach>

counter keeps tracking of the iteration count variable.

Note: Since the forEach loop depends on the size of the first list. This will not work correctly when the size of the depotList or userAuctionrelDolist is greater than the size of the first list

查看更多
等我变得足够好
4楼-- · 2019-08-20 04:56

Something like this

<c:forEach items="${auctionDOList}" var="list" varStatus="counter">
    ${list.auctionId} ${depotList[counter.count].depotName} ${userAuctionRelDolist2[counter.count].auctionId}
</c:forEach>
查看更多
登录 后发表回答