2011-09-28 64 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

享受!