JSTL continue, break inside foreach

Nazneen picture Nazneen · Sep 28, 2011 · Viewed 80.3k times · Source

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.

Answer

BalusC picture BalusC · Sep 28, 2011

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.