2013-07-25 160 views
4

我想要做这样的事情两个变量

<c:forEach var="item1" items="List1" var="item2" items="List2"> 
<p> ${item1} ${item2}</p> 
</c:forEach> 

一个解决方案JSP的foreach标签是通过两个列表进行迭代,如果两个大小相同

<c:forEach var="i" begin="0" end="$(fn:length(List1))"> 
<p> <%= List1.get(i) %> <%= List2.get(i)%> //wrong syntax 
</c:forEach> 

任何想法如何做到这一点。

回答

4

您可以致电varStatus.index获取当前回合的索引,然后将其用作第二个列表的查找。请注意0​​的长度,否则它会抛出异常。设置itemsList两者中的最大值。

<c:forEach var="element" items="${List1}" varStatus="status"> 
<p> 
    ${element} 
    ${List2[status.index]} 
</c:forEach> 
  1. Documentation
  2. How to avoid Java Code in JSP-Files?
+0

如何在索引i访问元素? –

+0

@AdityaKumar'$ {List [i]}'!!!! – NINCOMPOOP

0
Array is Frist List, and B is Second List and varStatus.index to get the index of the current round and then use it as a lookup for the second list. 
<c:forEach var="Array" items="${A}" varStatus="status"> 
<c:out value="${A}","${B[status.index]}"}/> 
</c:forEach> 
+0

你还可以添加解释吗? – Robert