现在的位置: 首页 > 综合 > 正文

java仿百度分页

2012年04月16日 ⁄ 综合 ⁄ 共 2284字 ⁄ 字号 评论关闭

最近因为业务需要,写了个仿百度的分页工具类,略有所感,贴出来供大家参考、指正

其实 分页工具类并不难写,主要弄清楚以下几件事,就可以轻松写出分页

1、当前页(nowPage)如何获得

2、总页数(totalPage)如何获得

3、页面上起始页(startPage)如何计算得出

4、页面上结束页(endPage)如何计算得出

5、查询的条件是什么?如何通过页面将查询条件赋给相应类

6、如何根据查询条件查询出对象数组?这个数组(一般用集合list来做)用来在页面上作循环打印

7、分页机制。我贴出的代码是我仿百度的分页机制。页面显示10个页码,每页显示5条数据。至于这个机制,自己研究一下就很容易懂得。大体意思就是,当前页总位于中间位置,显示页数是10

8、信心——相信自己一定能写出很好的分页!

import java.util.List;

import com.cmnet.www.bean.Page;

/**
 * 分页工具
 *
 * @author Jasper
 *
 */
public class Pagination {
 private int nowPage;// 当前页
 private int startPage;// 起始页
 private int endPage;// 结束页
 private int totalRecord;// 总记录数
 private int totalPage;// 总页数
 private List objects;// 根据条件查出的对象集合
 private Conditions conditions;// 查询条件
 public static final int PAGESIZE = 5;// 每页显示的条数
 public static final int SHOWPAGES = 10;// 每页显示的页数

 public Pagination() {

 }

 /**
  * 分页方法
  * 通过这个方法,得到两个数据——startPage和endPage
  * 页面上的页码就是根据这两个数据处理后显示
  * @param nowPage当前页
  * @param totalPage总页数
  */
 public void paginationTool(int nowPage, int totalPage) {
  this.nowPage = nowPage;
  this.totalPage = totalPage;
  /**
   * 计算startPage与endPage的值
   *
   */
  if (this.totalPage < SHOWPAGES) {
   /** if中是总页数小于SHOWPAGES的情况 */
   this.startPage = 1;
   this.endPage = totalPage;
  } else {
   /** else中是总页数大于SHOWPAGES的情况 */
   if (this.nowPage <= SHOWPAGES / 2 + 1) {
    this.startPage = 1;
    this.endPage = SHOWPAGES;
   } else {
    this.startPage = this.nowPage - (SHOWPAGES / 2);
    this.endPage = this.nowPage + (SHOWPAGES / 2 - 1);
    if (this.endPage >= this.totalPage) {
     this.endPage = this.totalPage;
     this.startPage = this.totalPage - SHOWPAGES + 1;
    }
   }
  }
 }

 public Conditions getConditions() {
  return conditions;
 }

 public void setConditions(Conditions conditions) {
  this.conditions = conditions;
 }

 public int getNowPage() {
  return nowPage;
 }

 public void setNowPage(int nowPage) {
  this.nowPage = nowPage;
 }

 public int getTotalRecord() {
  return totalRecord;
 }

 public void setTotalRecord(int totalRecord) {
  this.totalRecord = totalRecord;
 }

 public int getTotalPage() {
  return totalPage;
 }

 public void setTotalPage(int totalPage) {
  this.totalPage = totalPage;
 }

 public List getObjects() {
  return objects;
 }

 public void setObjects(List objects) {
  this.objects = objects;
 }

 public static int getShowpages() {
  return SHOWPAGES;
 }

 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;
 }

}

抱歉!评论已关闭.