how to code foreach involving 3 lists in jstl?

2019-08-20 04:01发布

问题:

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

回答1:

Something like this

<c:forEach items="${auctionDOList}" var="list" varStatus="counter">
    ${list.auctionId} ${depotList[counter.count].depotName} ${userAuctionRelDolist2[counter.count].auctionId}
</c:forEach>


回答2:

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



回答3:

you will need forEach to do this. Refer this links

http://www.tutorialspoint.com/jsp/jstl_core_foreach_tag.htm

http://www.crazysquirrel.com/computing/java/jsp/jstl-forEach.jspx

being more specific you'll need nested forEach

http://www.coderanch.com/t/288775/JSP/java/jstl-nested-foreach

http://www.roseindia.net/jsp/simple-jsp-example/NestedForEach.shtml