分页详讲

1、建立一个Page类

public class Page {
    private List records;//放取分页记录
    private int currentPageNum;//当前分页
    private int pageSize=10;//每页显示的条数
    private int totalRecords;//总记录条数
    private int totalPage;//总页数
    private int startIndex;//每页开始记录的索引
    private int prePageNum;//上一页
    private int nextPageNum;//下一页nextPageNum
    private int startPage;//开始页码
    private int endPage;//结束页码
    private String uri;
    public String getUri() {
        return uri;
    }
    public void setUri(String uri) {
        this.uri = uri;
    }
    public Page(int currentPageNum,int totalRecords)
    {
        this.currentPageNum=currentPageNum;
        this.totalRecords=totalRecords;
        /**
         * 记录的总页数
         */
        totalPage=totalRecords%pageSize==0?totalRecords%pageSize:totalRecords%pageSize+1;
         //计算每页开始记录的索引
        startIndex=(currentPageNum-1)*10;   
        if(totalPage>5)
        {
            startPage=currentPageNum-2;
            endPage=currentPageNum+2;
            if(startPage<1)
            {
                startPage=1;
                endPage=5;
            } else if(endPage>totalPage)
            {
                endPage=totalPage;
                startPage=totalPage-4;
            }
        }else 
        {
            startPage=1;
            endPage=totalPage;
        }
    }
    public List getRecords() {
        return records;
    }
    public void setRecords(List records) {
        this.records = records;
    }
    public int getCurrentPageNum() {
        return currentPageNum;
    }
    public void setCurrentPageNum(int currentPageNum) {
        this.currentPageNum = currentPageNum;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getTotalRecords() {
        return totalRecords;
    }
    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }
    public int getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getStartIdex() {
        return startIndex;
    }
    public void setStartIdex(int startIdex) {
        this.startIndex = startIdex;
    }
    public int getPrePageNum() {
        prePageNum=currentPageNum-1;
        if(prePageNum<1)
            prePageNum=1;
        return prePageNum;
    }
    public void setPrePageNum(int prePageNum) {
        this.prePageNum = prePageNum;
    }
    public int getNextPageNum() {
        nextPageNum=currentPageNum+1;
        if(nextPageNum>totalPage)
            nextPageNum=totalPage;
        return nextPageNum;
    }
    public void setNextPageNum(int nextPageNum) {
        this.nextPageNum = nextPageNum;
    }
    public int getStartPage() {
        return startPage;
    }
    public void setStartPage(int startPage) {
        this.startPage = startPage;
    }
    public int getEndPage() {
        return endPage;
    }
    public void setEndPage(int endPage) {
        this.endPage = endPage;
    }


}

2、查询数据的总数目

public int getTotalRecords()throws Exception;

3、返回限制查询结果集

public List<类名> findPageRecords(int startIndex,int pageSize)throws Exception;

4、用页数返回结果

        //接口
    public Page findPage(String num)throws Exception;
    //实现类
    public Page findPage(String num) throws Exception {
        // TODO Auto-generated method stub
        int pageNum=1;
        if(num!=null&&!"".equals(num))
        {
            pageNum=Integer.parseInt(num);
        }
        System.out.println("pageNum=="+pageNum);
        int totalRecords=groupDao.getTotalRecords();//获取记录数
        Page page=new Page(pageNum, totalRecords);
        //System.out.println("page.getStartIdex()=="+page.getStartIdex());
        List records=groupDao.findPageRecords(page.getStartIdex(),page.getPageSize());
        //System.out.println("records"+records);
        page.setRecords(records);   
        return page;
    }

5、前端代码

 <!--                      分页开始                                                     -->

    <c:forEach begin="${page.startPage}" end="${page.endPage}" var="num">
        <a href="${pageContext.request.contextPath}${page.uri}&num=${num}">${num}</a>
    </c:forEach>
   第${page.currentPageNum}页/共${page.totalPage }页&nbsp;&nbsp;
   <a href="${pageContext.request.contextPath}">首页</a> 
   <a href="${pageContext.request.contextPath}/servlet/Controller?op=showAllCustomers&num=${page.prePageNum}">上一页</a>
   <a href="${pageContext.request.contextPath}/servlet/Controller?op=showAllCustomers&num=${page.nextPageNum}">下一页</a> 
   <a href="${pageContext.request.contextPath}/servlet/Controller?op=showAllCustomers&num=${page.totalPage}">尾页</a>
   <!--                      分页结束                                                     --> 
   <select id="pagenum" onchange="jump(this)">
   <c:forEach begin="1" end="${page.totalPage}" var="num">
   <option value="${num}" ${num==page.currentPageNum?'selected="selected"': ''}>${num}</option>
   </c:forEach>
   </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值