分页详讲

来源:互联网 发布:seo搜索引擎是什么 编辑:程序博客网 时间:2024/06/05 18:33

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>