在其正文中使用 c:if 条件时,将 c:forEach 限制为最大值

Limit c:forEach to a maximum when using an c:if condition in its body

我对 if 条件有疑问。我从 DB 获取数据,所有内容都在 2 个网站 tables 上查看。网站上的表格应仅限于 3 行。在 DB 中有一个名为 'screen' 的列。 'screen' 可以是 'first'、'second' 或 'both'。 'first' 是第一个 table,'second' 是第二个 table,'both' 表示两个 table。例如,如果我将数据添加为 'first' 屏幕,它应该在第一个 table 中查看,如果我将数据添加为 'both',它应该在两个 table 中查看等等。我将其限制为 3 行,所以问题是如果我添加 3 个 'first' 屏幕和 1 个 'both' 屏幕,我在第一个 table 上看到 3 行,但在第二个屏幕上没有任何行。该代码用于第一个 table:

<c:forEach items="${foo}" var="c" varStatus="status">
      <c:if test="${status.count <= 3 && c.screen.equals('both') || status.count <= 3 && c.screen.equals('first')}">

            <tr><td>something_bla_bla</td></tr>

      </c:if>
 </c:forEach>

第二个 table 的代码几乎相同 - 差异仅 c.screen.equals('second') 添加 3 'first' 显示 3 行 - 没关系, 添加 3 'both' 显示 3 行 - 没关系, 但是,如果我添加 3 'first' 和 1 'both',那 'both' 记录应该在第二个 table 中查看,这不是因为那个 3 限制。

这只是统计你输出了多少行的问题。例如:

<c:set var="count" value="0"/>
<c:forTokens items="first,second,second,both,first,both,second"
             delims=","
             var="item">
  <c:if test="${count lt 3 and 'both,first'.contains(item)}">
    <c:out value="${item}"/>
    <c:set var="count" value="${count + 1}"/>
  </c:if>
</c:forTokens>

<br/>

<c:set var="count" value="0"/>
<c:forTokens items="first,second,second,both,first,both,second"
             delims=","
             var="item">
  <c:if test="${count lt 3 and 'both,second'.contains(item)}">
    <c:out value="${item}"/>
    <c:set var="count" value="${count + 1}"/>
  </c:if>
</c:forTokens>

这将输出(忽略空格):

first both first<br/>
second second both

请注意,我使用的 'both,first'.contains(item)(item eq 'both' or item eq 'first') 具有相同的效果,但更短(特别是如果您需要移动字符串进行检查)。