動態實現分頁

在開發網站的過程中,做分頁處理肯定是常常遇到的。這裏介紹一個對整個應用都有效的分頁方式。(WAP開發中,用不了JS,顯得更有用了)

 

在JSP頁面上,只需在需要顯示分頁信息的地方加入請求參數(參數值爲分頁代碼)。例如,在引入了struts2標籤的頁面中加入代碼:<s:property value="pageInfo.nav">。       在發送請求的servlet中,只需實例化pageInfo,並設置好需要跳轉的url(請求地址名:httpServletRequest.getRequestURI()、請求參數:httpServletRequest.getQueryString())。

 

   下面是用於分頁處理的Page類:

public class Page {
 private int numPerPage = 5; // 頁大小

 private int totalRows; // 總記錄數

 private int totalPages; // 總頁數

 private int currentPage; // 起始頁

 private int startIndex; // 起始行數

 private int lastIndex; // 結束行數

 private boolean hasNext; // 是否有下一頁

 private boolean hasPrev; // 是否有上一頁

 private int offset = 0; // 偏移量

 private String baseUrl = ""; //轉向的地址

 public Page() {
 }

 public Page(int currentPage,int totalRows){
  init(this.numPerPage,currentPage,totalRows,0);
 }
 public Page(int currentPage,int totalRows,int numPerPage)
 {
  init(numPerPage,currentPage,totalRows,0);
 } 
 public Page(int currentPage,int totalRows,int numPerPage,int offset)
 {
  init(numPerPage,currentPage,totalRows,offset);
 }
 // 初始化Page
 private void init(int numPerPage,int currentPage,int totalRows,int offset){
  this.numPerPage=numPerPage;
  this.currentPage=currentPage;
  this.totalRows=totalRows;
  this.offset=offset;
  this.setTotalPages();
  this.setHasNext();
  this.setHasPrev();
  this.setStartIndex();
  this.setLastIndex();
 }
 
 //構造分頁導航條   最後的字符串顯示到頁面上
 public String getNav(){
  String navStr="";
  if(this.isHasPrev()){
   navStr+="<a href=/""+this.getBaseUrl()+"&pageNum="+(this.getCurrentPage()-1)+"/">上一頁"+"</a> ";
  }
  if(this.hasNext){
   navStr+="<a href=/""+this.getBaseUrl()+"&pageNum="+(this.getCurrentPage()+1)+"/">下一頁"+"</a> ";
  }
  if(this.getTotalPages()>0){
   navStr+="第"+this.getCurrentPage()+"頁/共"+this.getTotalPages()+"頁";
  }
  return navStr;
 }
 
 public int getNumPerPage() {
  return numPerPage;
 }

 public void setNumPerPage(int numPerPage) {
  this.numPerPage = numPerPage;
 }

 public int getTotalRows() {
  return totalRows;
 }

 public void setTotalRows(int totalRows) {
  this.totalRows = totalRows;
 }

 public int getTotalPages() {
  return totalPages;
 }

 public void setTotalPages() {
  if(totalRows%numPerPage==0){   //正好n頁
   totalPages=totalRows/numPerPage;
  }else{
   totalPages=totalRows/numPerPage+1;
  }
  
  if(currentPage>totalPages){
   if(totalPages>0){
    currentPage=totalPages;
   }else{
    currentPage=1;
   }
  }
  
  if(currentPage<1){
   currentPage=1;
  }
 }

 public int getCurrentPage() {
  return currentPage;
 }

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

 public int getStartIndex() {
  return startIndex;
 }

 public void setStartIndex() {
  startIndex=(currentPage-1)*numPerPage+1;
 }

 public int getLastIndex() {
  return lastIndex;
 }

 public void setLastIndex() {
  lastIndex=currentPage*numPerPage;
  //lastIndex=startIndex+numPerPage-1
  if(lastIndex>totalRows){
   lastIndex=totalRows;
  }
 }

 public boolean isHasNext() {
  return hasNext;
 }

 public void setHasNext() {
  if(totalPages>currentPage){
   hasNext=true;
  }else{
   hasNext=false;
  }
 }

 public boolean isHasPrev() {
  return hasPrev;
 }

 public void setHasPrev() {
  if(currentPage>1){
   hasPrev=true;
  }else{
   hasPrev=false;
  }
 }

 public int getOffset() {
  return offset;
 }

 public void setOffset(int offset) {
  this.offset = offset;
 }

 public String getBaseUrl() {
  return baseUrl;
 }

  //設置“上一頁”“下一頁”轉向的地址。url:請求項目名,param:請求參數(名值對)

 public void setBaseUrl(String url,String param) {
  int index=param.indexOf("pageNum")-1;
  if(index>0){

    // 清除先前的頁面參數對,這裏只處理頁面信息在參數串末尾的情況。
   param=param.substring(0,index);
  }
  this.baseUrl = url+"?"+param;
 }

}

發佈了28 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章