PageBean.java
上传用户:zhihansy
上传日期:2014-12-04
资源大小:7241k
文件大小:5k
源码类别:

搜索引擎

开发平台:

Java

  1. package com.lucene;
  2. import com.lucene.*;
  3. import java.util.*;
  4. /**
  5.  * @作者 申华锋
  6.  * Struts分页显示逻辑Bean
  7.  */
  8. public class PageBean {
  9.  int currentPage=1;  //当前页
  10. public int totalPages=0;  //总页数
  11.  int pageRecorders=5;//每页5条数据
  12.  int totalRows=0;  //总数据数
  13.  int pageStartRow=0;//每页的起始数
  14.  int pageEndRow=0;  //每页显示数据的终止数
  15.  boolean hasNextPage=false; //是否有下一页
  16.  boolean hasPreviousPage=false; //是否有前一页
  17.  ArrayList arrayList;
  18.  Iterator it;
  19. public PageBean(){}
  20.  
  21. public PageBean(ArrayList arrayList){
  22.  this.arrayList=arrayList; 
  23.  totalRows=arrayList.size();  
  24.     it=arrayList.iterator();  
  25.  hasPreviousPage=false;
  26.  currentPage=1;
  27.  if((totalRows%pageRecorders)==0)
  28.  {
  29.  totalPages=totalRows/pageRecorders;  
  30.  }
  31.  else
  32.  {
  33.   totalPages=totalRows/pageRecorders+1; 
  34.  } 
  35.  
  36.  if(currentPage>=totalPages)  
  37.  {
  38.   hasNextPage=false; 
  39.  }
  40.  else                        
  41.  {
  42.   hasNextPage=true;
  43.  }
  44.     
  45.     if(totalRows<pageRecorders) 
  46.     {
  47.     this.pageStartRow=0;           
  48.     this.pageEndRow=totalRows;   
  49.     }
  50.     else                       
  51.     {
  52.     this.pageStartRow=0;         
  53.     this.pageEndRow=pageRecorders;   
  54.     }
  55. }
  56.  /**
  57.   * @return Returns the currentPage.
  58.   */
  59.  public String getCurrentPage() {
  60.   return this.toString(currentPage);
  61.  }
  62.  /**
  63.   * @param currentPage The currentPage to set.
  64.   */
  65.  public void setCurrentPage(int currentPage) {
  66.   this.currentPage = currentPage;
  67.  }
  68.  /**
  69.   * @return Returns the pageRecorders.
  70.   */
  71.  public int getPageRecorders() {
  72.   return pageRecorders;
  73.  }
  74.  /**
  75.   * @param pageRecorders The pageRecorders to set.
  76.   */
  77.  public void setPageRecorders(int pageRecorders) {
  78.   this.pageRecorders = pageRecorders;
  79.  }
  80.  /**
  81.   * @return Returns the pageEndRow.
  82.   */
  83.  public int getPageEndRow() {
  84.   return pageEndRow;
  85.  }
  86.  /**
  87.   * @return Returns the pageStartRow.
  88.   */
  89.  public int getPageStartRow() {
  90.   return pageStartRow;
  91.  }
  92.  /**
  93.   * @return Returns the totalPages.
  94.   */
  95.  public String getTotalPages() {
  96.  
  97.   return this.toString(totalPages);
  98.  }
  99.  /**
  100.   * @return Returns the totalRows.
  101.   */
  102.  public String getTotalRows() {
  103.   return this.toString(totalRows);
  104.  }
  105.  /**
  106.   * @return Returns the hasNextPage.
  107.   */
  108.  public boolean isHasNextPage() {
  109.   return hasNextPage;
  110.  }
  111.  /**
  112.   * @param hasNextPage The hasNextPage to set.
  113.   */
  114.  public void setHasNextPage(boolean hasNextPage) {
  115.   this.hasNextPage = hasNextPage;
  116.  }
  117.  /**
  118.   * @return Returns the hasPreviousPage.
  119.   */
  120.  public boolean isHasPreviousPage() {
  121.   return hasPreviousPage;
  122.  }
  123.  /**
  124.   * @param hasPreviousPage The hasPreviousPage to set.
  125.   */
  126.  public void setHasPreviousPage(boolean hasPreviousPage) {
  127.   this.hasPreviousPage = hasPreviousPage;
  128.  }
  129. public Product[] getNextPage(){
  130.  
  131.  currentPage=currentPage+1;
  132.  System.out.println("PageBean.getNextPage()正在执行;");
  133.  System.out.println("参数currentPage="+currentPage);
  134.  if((currentPage-1)>0)
  135.  {
  136.   hasPreviousPage=true; 
  137.  }
  138.     else
  139.     {
  140.      hasPreviousPage=false; 
  141.     }
  142.  
  143.  if(currentPage>=totalPages) 
  144.  {
  145.   hasNextPage=false; 
  146.  }
  147.  else
  148.  {
  149.   hasNextPage=true;
  150.  }
  151.  System.out.println("参数hasNextPage="+hasNextPage);
  152.  System.out.println("准备执行PageBean.getBooks()");
  153.  Product[] books=getBooks();
  154.  this.description();
  155.  
  156.  return books;
  157. }
  158. public Product[] getPreviouspage(){
  159.  
  160.  currentPage=currentPage-1;
  161.     if(currentPage==0){currentPage=1;}
  162.  
  163.  if(currentPage>=totalPages)  
  164.  {
  165.   hasNextPage=false; 
  166.  }
  167.  else                         
  168.  {
  169.   hasNextPage=true;
  170.  }
  171.  if((currentPage-1)>0)
  172.  {
  173.   hasPreviousPage=true; 
  174.  }
  175.     else
  176.     {
  177.      hasPreviousPage=false; 
  178.     }
  179.  Product[] books=getBooks();
  180.  this.description();
  181.  return books;
  182. }
  183. public Product[] getBooks(){
  184.  System.out.println("pageBean.getBooks()开始执行;");
  185.  
  186.  
  187.  if(currentPage*pageRecorders<totalRows){//判断是否为最后一页
  188.   pageEndRow=currentPage*pageRecorders;
  189.      pageStartRow=pageEndRow-pageRecorders;
  190.  }
  191.  else{
  192.   pageEndRow=totalRows;
  193.   pageStartRow=pageRecorders*(totalPages-1);
  194.  }
  195.  Product[] books=new Product[pageEndRow-pageStartRow+1];
  196.  
  197.  System.out.println("pageStartRow="+pageStartRow);
  198.  System.out.println("pageEndRow="+pageEndRow);
  199.   int j=0; 
  200.  for(int i=pageStartRow;i<pageEndRow;i++)
  201.  {
  202.  
  203.  Product book=(Product)arrayList.get(i); 
  204.   books[j++]=book;
  205.  
  206.  }
  207.  System.out.println("要显示的页面数据已经封装,具体信息如下:");
  208.  this.description();
  209.  return books;
  210. }
  211. public String toString(int temp)
  212. {
  213. String str=Integer.toString(temp);
  214. return str;
  215. }
  216. public void description()
  217. {
  218.    String description="共有数据数:"+this.getTotalRows()+
  219.    "共有页数: "+this.getTotalPages() +
  220.    "当前页数为:"+this.getCurrentPage()+
  221.    
  222.    " 是否有前一页: "+this.isHasPreviousPage() +
  223.    " 是否有下一页:"+this.isHasNextPage()+
  224.    " 开始行数:"+this.getPageStartRow()+
  225.    " 终止行数:"+this.getPageEndRow();
  226.    System.out.println(description);
  227. }
  228. }