JSTL continue, break inside foreach

86,693

Solution 1

There's no such thing. Just do the inverse for the content you actually want to display. So don't do

<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>

but rather do

<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>

or

<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>

Please note that I fixed an EL syntax error in your code as well.

Solution 2

Or you can use EL choose statement

<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>

Solution 3

I solved it using Set at the end of my executable code and inside of the loop

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

then I used that variable to skip the execution of the code in the next iteration using

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

you can set it back to true at any time...

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

more on this tag at: JSTL Core Tag

enjoy!

Share:
86,693

Related videos on Youtube

Nazneen
Author by

Nazneen

Im an IT professional working as Software Engineer

Updated on April 22, 2022

Comments

  • Nazneen
    Nazneen about 2 years

    I want to insert "continue" inside foreach in JSTL. Please let me know if there is a way to achieve this.

    <c:forEach 
      var="List"
      items="${requestScope.DetailList}" 
      varStatus="counter"
      begin="0">
    
      <c:if test="${List.someType == 'aaa' || 'AAA'}">
        <<<continue>>>
      </c:if>
    

    I want to insert the "continue" inside the if condition.

  • CoolBeans
    CoolBeans over 12 years
    +1 aah - now I understand why she wanted to use continue. Good interpretation of the question BalusC!
  • Nazneen
    Nazneen over 12 years
    I cant do the inverse. Because, I do some actions under the loop. I wanted to stop that if this condition pass. I want to go for the next iteration if this condition passes. Thanks for your answer. I'll try to go with another logic if there is no way of making it to next iteration with continue.
  • Nazneen
    Nazneen over 12 years
    I made the condition inverse and it works fine now. Thanks a lot for your replies and interest in support.