代码改变世界

java中分页的实现

2016-06-28 14:35  如是我所闻  阅读(1249)  评论(0编辑  收藏  举报

page实体类:

public class Page<T> {
	private int perPageNum;// 每页的数据条数
	private int pageNum;// 访问的哪一页
	private int totalRecordsCount;// 总的数据条数
	private int totalPageNum;// 总页数
	private List<T> pageT;//每一页的所有实体数据,T为实体

}

调用实例:  

    // 分页查询
    public Page pageQuery(int pageNum) throws Exception {
        int perPageNum = 10;// 每一页显示的数量
        int totalRecordCount = schoolDaoImpl.schoolCount();// 总记录
        int totalPageCount = (totalRecordCount + perPageNum - 1) / perPageNum;// 总页数
        // 获得数据
        List<School> schools = schoolDaoImpl.getSpecifyPageData((pageNum - 1) * perPageNum, perPageNum);

Page schoolPage
= new Page(); schoolPage.setPageNum(pageNum); schoolPage.setPageT(schools); schoolPage.setPerPageNum(perPageNum); schoolPage.setTotalPageNum(totalPageCount); schoolPage.setTotalRecordsCount(totalRecordCount); return schoolPage; }

 

JSP中的分页实现:ps:page由上方调用实例填充到request的结果

    <!-- 具体数据-->
    <table id="dynamic-table"
        class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th>专业名称</th>
                <th>专业代码</th>
                <th>创建时间</th>
            </tr>
        </thead>

        <tbody>
            <c:forEach items="${page.pageT}" var="major">
                <tr>
                    <td>${major.majorName}</td>
                    <td>${major.majorCode}</td>
                    <td>${major.createDate}</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>

    <!-- 数据下方的首页,第几页,尾页什么的-->
    <div class="center">
        第${page.pageNum}页/共${page.totalPageNum}页
        <c:if test="${page.pageNum>1}">
            <a
                href="<c:url value="/school/schIncluMajorsPage"/>?pageNum=1&schID=${jumpSchID}">首页</a>
            <a
                href="<c:url value="/school/schIncluMajorsPage"/>?pageNum=${page.pageNum-1}&schID=${jumpSchID}">上一页</a>
        </c:if>
        <%-- 计算begin、end --%>
        <c:choose>
            <%-- 如果总页数不足10页,那么把所有的页数都显示出来! --%>
            <c:when test="${page.pageNum <= 10 }">
                <c:set var="begin" value="1" />
                <c:set var="end" value="${page.totalPageNum}" />
            </c:when>
            <c:otherwise>
                <%-- 当总页数>10时,通过公式计算出begin和end --%>
                <c:set var="begin" value="${page.pageNum-5 }" />
                <c:set var="end" value="${page.pageNum+4 }" />
                <%-- 头溢出 --%>
                <c:if test="${begin < 1 }">
                    <c:set var="begin" value="1" />
                    <c:set var="end" value="10" />
                </c:if>
                <%-- 尾溢出 --%>
                <c:if test="${end > page.totalPageNum}">
                    <c:set var="begin" value="${page.totalPageNum - 9 }" />
                    <c:set var="end" value="${page.totalPageNum}" />
                </c:if>
            </c:otherwise>
        </c:choose>
        <%-- 循环遍历页码列表 --%>
        <c:forEach var="i" begin="${begin }" end="${end }">
            <c:choose>
                <c:when test="${i eq page.pageNum }">[${i }]</c:when>
                <c:otherwise>
                    <a
                        href="<c:url value="/school/schIncluMajorsPage"/>?pageNum=${i}&schID=${jumpSchID}">[${i }]</a>
                </c:otherwise>
            </c:choose>

        </c:forEach>


        <c:if test="${page.pageNum <page.totalPageNum }">
            <a
                href="<c:url value="/school/schIncluMajorsPage"/>?pageNum=${page.pageNum+1}&schID=${jumpSchID}">下一页</a>
            <a
                href="<c:url value="/school/schIncluMajorsPage"/>?pageNum=${page.totalPageNum}&schID=${jumpSchID}">尾页</a>
        </c:if>
    </div>