JSTL continue, break inside foreach

后端 未结 3 1311
梦毁少年i
梦毁少年i 2020-12-10 10:33

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



        
相关标签:
3条回答
  • 2020-12-10 10:56

    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.

    0 讨论(0)
  • 2020-12-10 11:04

    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!

    0 讨论(0)
  • 2020-12-10 11:20

    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>
    
    0 讨论(0)
提交回复
热议问题