首頁 php教程 PHP开发 簡單實作jsp分頁

簡單實作jsp分頁

Dec 29, 2016 pm 03:28 PM

資料分頁,在開發中必不可少,由於資料量太大,不得不進行處理。但對於分頁,又有很多種方式,例如使用sql語句的limit,或是使用jquery的外掛。但是底層的邏輯,都是需要的。 
在這裡貼出bean物件的程式碼,以及一個輔助類別。我做的這個專案是一個個人博客,算是個人網站吧。

(只貼部分底層邏輯程式碼)

Page.java

package cn.edu.ldu.util;
 
public class Page {
 
  private int everyPage;     //每页显示的数量
  private int totalPage;     //总页数
  private int totalCount;     //总数量
  private int currentPage;    //当前页
  private int beginIndex;     //起始页
  private boolean hasPrepage;   //是否有上一页
  private boolean hasNextPage;  //是否有下一页
 
  public Page() {
  }
 
  public Page(int everyPage, int totalPage, int totalCount, int currentPage,int beginIndex, boolean hasPrepage, boolean hasNextPage)
  {
    this.everyPage = everyPage;
    this.totalPage = totalPage;
    this.totalCount = totalCount;
    this.currentPage = currentPage;
    this.beginIndex = beginIndex;
    this.hasPrepage = hasPrepage;
    this.hasNextPage = hasNextPage;
  }
 
  public int getEveryPage() {
    return everyPage;
  }
 
  public void setEveryPage(int everyPage) {
    this.everyPage = everyPage;
  }
 
  public int getTotalPage() {
    return totalPage;
  }
 
  public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
  }
 
  public int getTotalCount() {
    return totalCount;
  }
 
  public void setTotalCount(int totalCount) {
    this.totalCount = totalCount;
  }
 
  public int getCurrentPage() {
    return currentPage;
  }
 
  public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
  }
 
  public int getBeginIndex() {
    return beginIndex;
  }
 
  public void setBeginIndex(int beginIndex) {
    this.beginIndex = beginIndex;
  }
 
  public boolean isHasPrepage() {
    return hasPrepage;
  }
 
  public void setHasPrepage(boolean hasPrepage) {
    this.hasPrepage = hasPrepage;
  }
 
  public boolean isHasNextPage() {
    return hasNextPage;
  }
 
  public void setHasNextPage(boolean hasNextPage) {
    this.hasNextPage = hasNextPage;
  }
 
}
登入後複製

PageUtil.java

package cn.edu.ldu.util;
 
public class PageUtil {
  //创建分页信息对象
  public static Page createPage(int everyPage,int totalCount,int currentPage) {
    everyPage = getEveryPage(everyPage);
    currentPage = getCurrentPage(currentPage);
    int totalPage = getTotalPage(everyPage, totalCount);
    int beginIndex = getBeginIndex(everyPage, currentPage);
    boolean hasPrePage = getHasPrePage(currentPage);
    boolean hasNextPage = getHasNextPage(totalPage, currentPage);
    return new Page(everyPage, totalPage,totalCount, currentPage,
        beginIndex, hasPrePage, hasNextPage);
  }
 
  //获得每页显示记录数
  public static int getEveryPage(int everyPage) {
    return everyPage == 0 ? 10 : everyPage;
  }
 
  //获得当前页
  public static int getCurrentPage(int currentPage) {
    return currentPage == 0 ? 10 : currentPage;
  }
 
  //获得总页数
  public static int getTotalPage(int everyPage,int totalCount) {
    int totalPage=0;
    //要判断每页是否满页,否则页数+1
    if(totalCount!=0 && totalCount%everyPage==0) {
      totalPage=totalCount/everyPage;
    }
    else {
      totalPage=totalCount/everyPage+1;
    }
    return totalPage;
  }
 
  //获得起始位置
  public static int getBeginIndex(int everyPage,int currentPage) {
    return everyPage*(currentPage-1);
  }
 
  //判断是否有上一页
  public static boolean getHasPrePage(int currentPage) {
    return currentPage== 1 ? false : true;
 
  }
 
  //判断是否有下一页
  public static boolean getHasNextPage(int totalPage, int currentPage) {
    return (currentPage == totalPage) || (totalPage == 0) ? false : true;
  }
 
}
登入後複製

每次我只需要去呼叫即可。

//传入每页显示的页数,总记录数,当前页
Page page=PageUtil.createPage(5, diaryDao.findAllCount(), currentPage);
登入後複製

簡單實作jsp分頁

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支PHP中文網。


更多簡單實現jsp分頁相關文章請關注PHP中文網!


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1665
14
CakePHP 教程
1424
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24