【问题标题】:Convert for loop to JSTL/EL expression将 for 循环转换为 JSTL/EL 表达式
【发布时间】:2014-04-11 10:03:57
【问题描述】:

我有这个循环

    for(int i = 1 + offset; i <= pageSetPerBatch + offset && (i - 1) * resultPerPage < records; i++) {
        if(currentPage == i) {
            System.out.println(i + "(Current)");
        }
        else {
            System.out.println(i);
        }
    }   

现在,我知道如何使用&lt;c:forEach&gt; 对集合进行迭代,但问题是,如何将循环条件转换为 JSTL/EL 语法?顺便说一下,这是一个页码。

只是胡闹,我试过了

<c:forEach begin="1" end="100" var="val">
    <c:out value="${val}"/>
</c:forEach>

但这不是我想要做的。

【问题讨论】:

    标签: java jsp jstl el


    【解决方案1】:
    <c:forEach begin="1" end="100" var="val" varStatus="status">
       <c:choose>
          <c:when test="${currentPage == status.index}">
             <c:out value="${status.index} (Current)"/>
          </c:when>
          <c:otherwise>
             <c:out value="${status.index}"/>
          </c:otherwise>
       </c:choose>
    </c:forEach>
    

    但是你不应该在前端使用复杂的逻辑。它应该在后端完成。

    【讨论】:

    • 如何设置结束以获取循环的当前索引?
    • ${status.index} - 循环中的当前索引。
    猜你喜欢
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 2012-08-10
    • 2013-03-05
    相关资源
    最近更新 更多