2010-11-10 91 views

回答

26

您可以拨打varStatus.index获得本轮迭代的索引,然后把它作为第二个列表中查找。

例如,如果你有两个列表people.firstnamespeople.lastnames你可以这样做:

<c:forEach var="p" items="${people.firstnames}" varStatus="status"> 
    <tr> 
     <td>${p}</td> 
     <td>${people.lastnames[status.index]}</td> 
    </tr> 
</c:forEach> 
+0

非常直接,聪明的做法。 +1 – 2010-11-10 09:36:12

+0

谢谢。这工作。 – randy 2010-11-10 09:47:52

0

我认为你必须收集你想要一次迭代的集合。添加一个将合并这两个集合的getter并将其用于迭代。例如

private Collection<String> first; 
private Collection<String> second; 

public Collection<String> getBoth() 
{ 
    List<String> result = new ArrayList<String>(); 
    result.addAll(first); 
    result.addAll(second); 
    return result; 
} 

迭代中JSTL:

<c:forEach var="p" items="${people.both}"> 
    <tr> 
     <td>${p}</td> 
    </tr> 
</c:forEach> 
+0

用这个,我怎么能重复第一和使用JSTL的foreach第二藏品?感谢boris – randy 2010-11-10 08:55:38

+0

使用getBoth()的结果??? – ZeissS 2010-11-10 09:20:06

相关问题