java仿百度分頁

來源:互聯網
上載者:User

最近因為業務需要,寫了個仿百度的分頁工具類,略有所感,貼出來供大家參考、指正

其實 分頁工具類並不難寫,主要弄清楚以下幾件事,就可以輕鬆寫出分頁

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

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.