2011-09-28 47 views
18

我想在JSTL裏插入foreach裏的「continue」。請讓我知道是否有辦法做到這一點。JSTL繼續,破解裏面的foreach

<c:forEach 
    var="List" 
    items="${requestScope.DetailList}" 
    varStatus="counter" 
    begin="0"> 

    <c:if test="${List.someType == 'aaa' || 'AAA'}"> 
    <<<continue>>> 
    </c:if> 

我想在if條件中插入「continue」。

回答

27

有沒有這樣的事情。只需對你想要顯示的實際上的內容進行相反處理即可。所以,不要做

<c:forEach items="${requestScope.DetailList}" var="list"> 
    <c:if test="${list.someType eq 'aaa' or list.someType eq 'AAA'}"> 
     <<<continue>>> 
    </c:if> 
    <p>someType is not aaa or AAA</p> 
</c:forEach> 

而是做

<c:forEach items="${requestScope.DetailList}" var="list"> 
    <c:if test="${not (list.someType eq 'aaa' or list.someType eq 'AAA')}"> 
     <p>someType is not aaa or AAA</p> 
    </c:if> 
</c:forEach> 

<c:forEach items="${requestScope.DetailList}" var="list"> 
    <c:if test="${list.someType ne 'aaa' and list.someType ne 'AAA'}"> 
     <p>someType is not aaa or AAA</p> 
    </c:if> 
</c:forEach> 

請注意,我在你的代碼修正了EL語法錯誤也是如此。

+0

+1 aah - 現在我明白她爲什麼要繼續使用了。對BalusC問題的好解釋! – CoolBeans

+0

我不能做相反的事。因爲,我在循環中做了一些動作。如果這種情況通過,我想阻止它。如果這個條件通過,我想去下一個迭代。感謝您的回答。如果沒有辦法繼續進行下一次迭代,我會嘗試使用另一種邏輯。 – Nazneen

+0

隨意用具體邏輯編輯問題。 – BalusC

2

或者你可以使用EL 選擇聲明

<c:forEach 
     var="List" 
     items="${requestScope.DetailList}" 
     varStatus="counter" 
     begin="0"> 

     <c:choose> 
     <c:when test="${List.someType == 'aaa' || 'AAA'}"> 
      <!-- continue --> 
     </c:when> 
     <c:otherwise> 
      Do something...  
     </c:otherwise> 
     <c:choose> 
    </c:forEach> 
3

我來回答你的問題的「破發」的一部分,因爲其他的答案集中在「繼續」(這的確是不可能的) 。 這不是一個真正的「休息」,因爲後來進來同一迴路一步一切仍將進行評估,但你可以通過快捷鍵循環如下:

<c:forEach var="apple" items="${apples}" varStatus="status"> 
    <c:if test="${apple eq pear}"> 
     ...do stuff with this apple... 
     <c:set var="status.index" value="${items.size}"/> <%-- 'break' out of loop --%> 
     ... stuff here will still be evaluated... 
    </c:if> 
    ... stuff here will still be evaluated... 
</c:forEach> 

所以,如果你不需要你要休息跳過一些代碼,這對你仍然有用。 當然,一般來說,它是修改循環內循環索引的BAD-mkay,但這很好。

+0

我嘗試了這種方法,但它對我無效。我檢查了一下,在'

0

我解決它使用設置在我的可執行代碼的結束和內環路

<c:set var="continueExecuting" scope="request" value="false"/> 

然後我用這個變量使用跳過代碼的下一次迭代中執行

<c:if test="${continueExecuting}"> 

,你可以在任何時間將其設置回真的...

<c:set var="continueExecuting" scope="request" value="true"/> 

更多關於這個標籤在:JSTL Core Tag

享受!