如何做分页逻辑

时间:2013-11-05 06:56:12

标签: java hibernate pagination

任何人都可以提供一些想法/逻辑来为页面编写分页逻辑,以便使用hibernate将每个页面的指定记录拉到精确的数据库中。我所拥有的信息是该搜索的总页数 - 每页10条记录我也被发送了前一页和下一页的编号(没有问题写我需要做的逻辑我拉这些信息并填充。我是还获取我所在页面的信息。我只能显示10页,如下所示

<previous 1 |2 |3 | 4| 5 | 6 | 7 | 8 | 9 | 10 next>

假设总页数是15,当用户点击最后10时,则需要显示如下

<previous 2 |3 |4 |5 |6 |7 |8 |9 |10 |11 next> 

这是我的jsp

<c:forEach var="status1" items="${list1}">
                                        <c:set var="wins" ><fmt:parseNumber type="number" value="${status1.noOfPages}" /></c:set>
                                        <c:set var="wins1" ><fmt:parseNumber type="number" value="${status1. currentpage}" /></c:set>
                                        <%int i = 0;%>
                                        <%int k = 0;%>
                                        <%int l = 1;%>
                                        <c:choose>
                                            <c:when test="${wins1 eq 1}">
                                                <a style="font-size: 18px;" href="#">[First]</a>
                                            </c:when>
                                            <c:otherwise>
                                                <a style="font-size: 18px;" href=LibraryBookList.do?pageIndex=<%=l%>>[First]</a>
                                            </c:otherwise>
                                        </c:choose>
                                        <c:choose>
                                            <c:when test="${wins1 eq 1}">
                                                <a href="#">[Previous]</a>
                                            </c:when>
                                            <c:otherwise>
                                                <a href="LibraryBookList.do?pageIndex=${wins1-1}">[Previous]</a>
                                            </c:otherwise>
                                        </c:choose>
                                        <c:forEach begin="1" end="${wins}" varStatus="loop">
                                            <a style="font-size: 18px;" href=LibraryBookList.do?pageIndex=<%=++k%>><%=++i%></a>
                                        </c:forEach>
                                        <c:choose>
                                            <c:when test="${wins1 eq wins}">
                                                <a href="#">[Next]</a>
                                            </c:when>
                                            <c:otherwise>
                                                <a href="LibraryBookList.do?pageIndex=${wins1+1}">[Next]</a>
                                            </c:otherwise>
                                        </c:choose>
                                        <c:choose>
                                            <c:when test="${wins1 eq wins}">
                                                <a style="font-size: 18px;" href="#"/>>[Last]</a>
                                            </c:when>
                                            <c:otherwise>
                                                <a style="font-size: 18px;" href=LibraryBookList.do?pageIndex=<c:out value="${wins}"/>>[Last]</a>
                                            </c:otherwise>
                                        </c:choose>
                                    </c:forEach>

3 个答案:

答案 0 :(得分:0)

我曾经尝试过一次我试过的解决方案:

<%--For displaying Previous link except for the 1st page --%>
    <c:if test="${currentPage != 1}">
        <td><a href="employee.do?page=${currentPage - 1}">Previous</a></td>
    </c:if>

    <%--For displaying Page numbers.
    The when condition does not display a link for the current page--%>
    <table border="1" cellpadding="5" cellspacing="5">
        <tr>
            <c:forEach begin="1" end="${noOfPages}" var="i">
                <c:choose>
                    <c:when test="${currentPage eq i}">
                        <td>${i}</td>
                    </c:when>
                    <c:otherwise>
                        <td><a href="employee.do?page=${i}">${i}</a></td>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
        </tr>
    </table>

    <%--For displaying Next link --%>
    <c:if test="${currentPage lt noOfPages}">
        <td><a href="employee.do?page=${currentPage + 1}">Next</a></td>
    </c:if>

我希望我的解决方案可以帮到你。

答案 1 :(得分:0)

如果您可以访问所有必填字段,例如pageNo,则NoOfRows将显示在每个页面中。每次用户单击任何页码时,您只需在服务器端休眠标准中使用以下逻辑。

criteria.setFirstResult((this.page - 1) * this.rows);
criteria.setMaxResults(this.rows.intValue());       
this.searchResults = criteria.list();

答案 2 :(得分:0)

我这样做了,它在Oracle ATG Commerce中有效。

<fmt:parseNumber var="totalPages" integerOnly="true" type="number" value="${totalOrdersCount/howMany}" />
                            <fmt:parseNumber var="stNo" integerOnly="true" type="number" value="1" />
                            <c:if test="${pgNo > 5 }">
                                <fmt:parseNumber var="x" integerOnly="true" type="number" value="${(pgNo%5)}" />
                                <fmt:parseNumber var="y" integerOnly="true" type="number" value="${(pgNo/5)-1}" />
                                <fmt:parseNumber var="stNo" integerOnly="true" type="number" value="${1+(y*5)+x}" />
                            </c:if>
                            <fmt:parseNumber var="endNo" integerOnly="true" type="number" value="${stNo+5-1}" />
                            <c:if test="${endNo >= totalPages}">
                                <fmt:parseNumber var="endNo" integerOnly="true" type="number" value="${totalPages+1}" />
                            </c:if>
                            <div class="grid-footer">
                                <div class="pagination help-main-pagination product-pagination">
                                    <ul class="pull-right">
                                        <c:if test="${(pgNo - 1) >= 1}">
                                            <li><a class="prev control" href="/lwoms/list/index.jsp?pgNo=${pgNo - 1}">Prev</a></li>
                                        </c:if>
                                         <c:forEach begin="${stNo}" end="${endNo}" var="i">
                                        <%--    <li><a class="active" href="/lwoms/list/index.jsp?pgNo=<c:out value="${i}"/>"><c:out value="${i}"/></a></li> --%>
                                            <c:choose>
                                                <c:when test="${pgNo eq i}">
                                                    <li><a class="active" href="/lwoms/list/index.jsp?pgNo=<c:out value="${i}"/>"><c:out value="${i}"/></a></li>
                                                </c:when>
                                                <c:otherwise>
                                                    <li><a href="/lwoms/list/index.jsp?pgNo=<c:out value="${i}"/>"><c:out value="${i}"/></a></li>
                                                </c:otherwise>
                                            </c:choose>
                                        </c:forEach>
                                        <c:if test="${(pgNo + 1) <= totalPages}">
                                            <li><a class="next control" href="/lwoms/list/index.jsp?pgNo=${pgNo + 1}">Next</a></li>
                                        </c:if>
                                    </ul>
                                </div>
                            </div>

希望这有帮助!