JSP JSTL继续,在foreach内部中断

tf7tbtn2  于 2022-12-07  发布在  其他
关注(0)|答案(4)|浏览(169)

我想在JSTL中的foreach里面插入“continue”。请让我知道是否有办法实现这一点。

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

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

我想在if条件中插入“continue”。

gpfsuwkq

gpfsuwkq1#

没有这样的事情。只要对你 * 实际 * 想要显示的内容做相反的操作就可以了。所以不要

<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语法错误。

bxjv4tth

bxjv4tth2#

我在可执行代码的末尾和循环内部使用Set解决了这个问题

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

然后我使用该变量在下一次迭代中跳过代码的执行,方法是使用

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

您可以随时将其设回true ...

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

有关此标签的更多信息,请访问:JSTL Core Tag
好好享受吧!

3hvapo4f

3hvapo4f3#

或者可以用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>
dy1byipe

dy1byipe4#

我喜欢这样的想法:在foreach上设置变量,并将其设置为循环的末尾,以便在构造一个开始和end for循环时使用c:set退出。
https://www.codesenior.com/en/tutorial/How-To-Break-Foreach-Loop-in-JSP

相关问题