Java 分页

  所在公司有分页组件,不过感觉没有网上看到的这个好,绑了很多公司业务上的东东再里面,所以还是大义灭亲地不说公司那个东东了,将网上看到的稍作记录,以备不时之需。

  首先,最主要的一个类是 PageView ,其中封装了构造一个页面所需全部信息。如下:

public class PageView<T> {

    /** 分页数据 **/

    private List<T> records;

    /** 页码开始索引和结束索引 **/

    private PageIndex pageindex;

    /** 总页数 **/

    private long totalpage = 1;

    /** 每页显示记录数 **/

    private int maxresult = 12;

    /** 当前页 **/

    private int currentpage = 1;

    /** 总记录数 **/

    private long totalrecord;

    /** 页码数量 **/

    private int pagecode = 10;

   

    public PageView(int maxresult, int currentpage) {

       this.maxresult = maxresult;

       this.currentpage = currentpage;

    }

   

    public void setQueryResult(QueryRst qr){

       setTotalrecord(qr.getTotalRecord());

       setRecords(qr.getRstList());

    }

   

    public int getPagecode() {

       return pagecode;

    }

 

    public void setPagecode(int pagecode) {

       this.pagecode = pagecode;

    }

 

    public long getTotalrecord() {

       return totalrecord;

    }

    public void setTotalrecord(long totalrecord) {

       this.totalrecord = totalrecord;

       setTotalpage(this.totalrecord%this.maxresult==0? this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1);

    }

    public List<T> getRecords() {

       return records;

    }

    public void setRecords(List<T> records) {

       this.records = records;

    }

    public PageIndex getPageindex() {

       return pageindex;

    }

    public long getTotalpage() {

       return totalpage;

    }

    public void setTotalpage(long totalpage) {

       this.totalpage = totalpage;

       this.pageindex = WebTool.getPageIndex(pagecode, currentpage, totalpage);

    }

    public int getMaxresult() {

       return maxresult;

    }

    public int getCurrentpage() {

       return currentpage;

    }

}

其中PageIndex,说明页码开始索引和结束索引

public class PageIndex {

    private long startindex;

    private long endindex;

   

    public PageIndex(long startindex, long endindex) {

       this.startindex = startindex;

       this.endindex = endindex;

    }

    public long getStartindex() {

       return startindex;

    }

    public void setStartindex(long startindex) {

       this.startindex = startindex;

    }

    public long getEndindex() {

       return endindex;

    }

    public void setEndindex(long endindex) {

       this.endindex = endindex;

    }

   

}

涉及到的getPageIndex方法为,用于确定当到达currenPage时的PageIndex状况,如下:

  public static PageIndex getPageIndex(long viewpagecount, int currenPage, long totalpage){

       long startpage = currenPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);

       long endpage = currenPage+viewpagecount/2;

       if(startpage<1){

           startpage = 1;

           if(totalpage>=viewpagecount) endpage = viewpagecount;

           else endpage = totalpage;

       }

        if(endpage>totalpage){

           endpage = totalpage;

           if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;

           else startpage = 1;

       }

       return new PageIndex(startpage, endpage);    

  }

另外,将结果集总记录数放在QueryRst里面

public class QueryRst<T> {

    private List<T> rstList;

    private long totalrecord;

    // getter & setter ...

}

使用时,

PageView<?> pageView = new PageView<?>(12,currentPage);

pageView.setQueryResult(qr);

posted @ 2011-02-20 22:19  sarafill  阅读(1069)  评论(0编辑  收藏  举报