java分頁代碼實現2

來源:互聯網
上載者:User

 public class PageSplit {
 int page = 1;// 請求顯第page頁
 int count = 1; // 記錄總數
 int currentPage = 1;// 實際顯示currentPage頁
 int pageCount = 1;// 總頁面數
 int rows = 1; // 每頁顯示的行數
 List list = null;

 /**
    * 構造方法一
    *
    * @param list準備進行分頁的List對象
    */
 public PageSplit(List list) {
    super();
    this.list = list;
 }

 /**
    * 構造方法二
    *
    * @param rows
    *             每頁顯示的行數
    * @param list
    *             準備進行分頁的List對象
    */
 public PageSplit(int rows, List list) {
    super();
    this.rows = rows;
    this.list = list;
 }

 /**
    * 返回第page頁的所有內容,結果仍為List型
    *
    * @param page返回第page頁的內容
    * @return
    */
 public List currentList(int page) {
    this.page = page;
    currentPage = page;
    count = list.size();
    pageCount = (count + rows - 1) / rows; // 根據你的紀錄總數,顯示行數,來計算分多少頁

    /* 如果請求頁小於1,則讓當前頁面數等於1 */
    if (currentPage < 1) {
     currentPage = 1;
    }
    /* 如果請求頁大於總頁數,則讓當前頁面數等於總頁數 */
    if (currentPage > pageCount) {
     currentPage = pageCount;
    }

    List newList;
    /* 不是滿頁的情況 */
    if ((((currentPage - 1) * rows) + rows) > count) {
     return newList = list.subList((currentPage - 1) * rows, count);
    }
    /* 恰巧每頁都滿的情況 */
    else {
     return newList = list.subList((currentPage - 1) * rows,
       ((currentPage - 1) * rows) + rows);
    }
 }

 public int getPage() {
    return page;
 }

 public void setPage(int page) {
    this.page = page;
 }

 public int getCount() {
    return count;
 }

 public void setCount(int count) {
    this.count = count;
 }

 public int getCurrentPage() {
    return currentPage;
 }

 public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
 }

 public int getPageCount() {
    return pageCount;
 }

 public void setPageCount(int pageCount) {
    this.pageCount = pageCount;
 }

 public int getRows() {
    return rows;
 }

 public void setRows(int rows) {
    this.rows = rows;
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.