【问题标题】:JSP and pagination linksJSP 和分页链接
【发布时间】:2016-01-19 17:22:44
【问题描述】:

我想以这种方式显示一个分页链接:当前页面和3个向前链接和3个向后链接。

显然我不想显示负面页面或最后一个页面之后的页面的链接。

所以在我的 pagination.jsp 中,我有以下内容: param.pages 是总页数 没有<c:if test="${page < param.pages} "> 的东西,我可以正确显示链接,但在 param.pages 之后没有检查页面。使用 c:if,我什么都不显示,我认为这是因为测试无法识别 page

    <c:forEach begin="${currentPage + 1 }" end="${currentPage + 3}" var="page">
            <c:if test="${page < param.pages} ">
            <li><a
                    href="${pageContext.request.contextPath}/${param.currentURL }/<c:out value="${page }"/>">
<c:out value="${page }"/> </a></li>
            </c:if>
            </c:forEach>

哪种方式最好?

【问题讨论】:

  • 你可以看看this demo。我用百里香叶。但你会弄清楚的。计算将显示哪些链接的逻辑在 Pager 类中。

标签: jsp pagination


【解决方案1】:

终于找到了,只用了jsp标签,不知道是不是最好的解决办法,但是好用,有需要的我会写下来。

在我的 pagination.jsp 中

<c:set var="maxLink" value="4"></c:set> // it's the number of links you want before and after the current one

// this part is to avoid to have negative values in the forEach cycle for the previous links
<c:set var="bottomLimit" value="${param.currentPage - maxLink }"></c:set>

        <c:choose>
            <c:when test="${bottomLimit <= 0 }">
                <c:set var="begin" value="1"></c:set>
            </c:when>
            <c:otherwise>
                <c:set var="begin" value="${bottomLimit }"></c:set>
            </c:otherwise>
        </c:choose>

<c:forEach begin="${begin }" end="${param.currentPage -1}"
            var="pageBefore">

            <li><a
                href="${pageContext.request.contextPath}/${param.currentURL }/<c:out value="${pageBefore }"/>"><c:out
                        value="${pageBefore }" /> </a></li>

        </c:forEach>

// link to the currentPage
<li class="active"><a
            href="${pageContext.request.contextPath}/${param.currentURL }/${param.currentPage}">${param.currentPage }</a></li>

//Link to the next pages, that ends at the last page
<c:set var="end" value="${param.currentPage + maxLink }"></c:set>
<c:forEach begin="${param.currentPage + 1 }" end="${end}" var="page">
            <c:if test="${page <= param.pages}">
                <li><a
                    href="${pageContext.request.contextPath}/${param.currentURL }/<c:out value="${page }"/>"><c:out
                            value="${page }" /> </a></li>
            </c:if>
        </c:forEach>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 2017-07-18
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    相关资源
    最近更新 更多