2013-03-03 100 views
2

我需要遍歷項目,但我也在尋找一種方法來獲取項目列表中的下一個或上一個元素,以便您可以在下面看到,我可以檢查第二個項目從第一項開始。有沒有一種方法可以像List或Array一樣存儲元素?JSTL ForEach循環/數組或列表

<c:forEach var="item" items="${entry.value.options}">       
        <c:set var="upper" value="${item.key }"/> 
        <c:choose> 
         <c:when test="${fn:startsWith(item, upper)}"> 
          <c:if test="${item ne upper} }"> 
           <ul> 
            <li> 
             <input class="parent" type="checkbox" name="criterias[${entry.key}]" value="${item.key}" /> 
             <label>${item.value}</label> 
            </li>          
           </ul> 
          </c:if> 
         </c:when> 
         <c:otherwise> 
         <li> 
          <input class="child" type="checkbox" name="criterias[${entry.key}]" value="${item.key}" /> 
          <label>${item.value}</label> 
         </li>  
         </c:otherwise> 
        </c:choose>    
    </c:forEach>  

非常感謝!

回答

2

您可以傳遞給jsp一個像List<Item> list這樣的結構,其中Item擁有自己的ArrayList,然後使用inner forEach。這可能比擁有扁平結構更容易。

<c:forEach var="item" items="${list}"> 
Access here item if needed <c:out value="${item.value}"/> 
    <c:forEach var="elem" items="${item}"> 
    ... 
    Access here elem <c:out value="${elem.value}"/> 
    ... 
    </c:forEach> 
</c:forEach> 
+0

嗨阿列克謝,對不起,我不明白。我如何比較列表中的前一個和下一個元素..在我的情況下,entry.value.options已經是一個列表。我無法在後端進行任何更改。 – NewQueries 2013-03-04 01:02:52