在JSP/JSTL中打破forEach循环

Dar*_*han 5 jsp jstl

我有一组具有一些属性的对象.一个属性只能是两种类型的要么Type1Type2.

我想要做的就是检查对象是否具有属性Type1.

我正在使用以下代码:

<c:set var="properties" value="${obj.getProperties()}"/>
<c:set var="hasPropertyOfType1" value="false"/>
<c:forEach var="i" begin="0" end="${fn:length(properties) - 1}">
    <c:if test="${properties.get(i).isOfType1() eq true}">
        <c:set var="hasPropertyOfType1" value="true"/>
    </c:if>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)

上面的代码实现了我想要的但是效率非常低,因为对象可能具有的属性数量很高.

所以我的问题是如果设置hasPropertyOfType1等于的话,如何打破forEach循环true.

我想到的一些方法,我不知道如何实现(或者它们甚至可能):

1:向forEach循环添加一个条件,这将停止循环.
2:将循环内的索引值增加到大于的值fn:length(properties).
3:如果可能,使用break语句.

我已经搜索了很多但仍未能解决这个问题.提前致谢.

小智 0

JSTL 标签用于迭代集合。它并不是为了搜索集合。

您也可以使用<c:choose>with <c:when><c:otherwise> 这里有一个例子。你也可以使用很多

<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>
Run Code Online (Sandbox Code Playgroud)

希望我能有所帮助