struts+hibernate的分頁代碼

package com.sict.employeeManagement.help;
import java.util.ArrayList;
import java.util.Iterator;

import com.sict.employeeManagement.data.SzEmployee;


public class PageBean {
int currentPage=1; //當前頁
public int totalPages=0; //總頁數
int pageRecorders=10;//每頁5條數據
int totalRows=0; //總數據數
int pageStartRow=0;//每頁的起始數
int pageEndRow=0; //每頁顯示數據的終止數
boolean hasNextPage=false; //是否有下一頁
boolean hasPreviousPage=false; //是否有前一頁
ArrayList arrayList;
Iterator it;
public PageBean(){}

public PageBean(ArrayList arrayList){
this.arrayList=arrayList;
totalRows=arrayList.size();
// System.out.print(totalRows);
it=arrayList.iterator();
hasPreviousPage=false;
currentPage=1;
if((totalRows%pageRecorders)==0)
{
totalPages=totalRows/pageRecorders;
}
else
{
totalPages=totalRows/pageRecorders+1;
}

if(currentPage>=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}


if(totalRows<pageRecorders)
{
this.pageStartRow=0;
this.pageEndRow=totalRows;
}
else
{
this.pageStartRow=0;
this.pageEndRow=pageRecorders;
}

}

/**
* @return Returns the currentPage.
*/
public String getCurrentPage() {
return this.toString(currentPage);
}
/**
* @param currentPage The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return Returns the pageRecorders.
*/
public int getPageRecorders() {
return pageRecorders;
}
/**
* @param pageRecorders The pageRecorders to set.
*/
public void setPageRecorders(int pageRecorders) {
this.pageRecorders = pageRecorders;
}
/**
* @return Returns the pageEndRow.
*/
public int getPageEndRow() {
return pageEndRow;
}
/**
* @return Returns the pageStartRow.
*/
public int getPageStartRow() {
return pageStartRow;
}
/**
* @return Returns the totalPages.
*/
public String getTotalPages() {

return this.toString(totalPages);
}
/**
* @return Returns the totalRows.
*/
public String getTotalRows() {
return this.toString(totalRows);
}
/**
* @return Returns the hasNextPage.
*/
public boolean isHasNextPage() {
return hasNextPage;
}
/**
* @param hasNextPage The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/**
* @return Returns the hasPreviousPage.
*/
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
/**
* @param hasPreviousPage The hasPreviousPage to set.
*/
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public SzEmployee[] getNextPage(){

currentPage=currentPage+1;
// System.out.println("PageBean.getNextPage()正在執行;");
// System.out.println("參數currentPage="+currentPage);

if((currentPage-1)>0)
{
hasPreviousPage=true;
}
else
{
hasPreviousPage=false;
}

if(currentPage>=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
// System.out.println("參數hasNextPage="+hasNextPage);
// System.out.println("準備執行PageBean.getBooks()");
SzEmployee[] employees=getSzEmployees();
this.description();

return employees;
}

public SzEmployee[] getPreviouspage(){

currentPage=currentPage-1;

if(currentPage==0){currentPage=1;}

if(currentPage>=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
if((currentPage-1)>0)
{
hasPreviousPage=true;
}
else
{
hasPreviousPage=false;
}
SzEmployee[] employees=getSzEmployees();
this.description();
return employees;
}

public SzEmployee[] getFirstpage(){

currentPage=1;

if(currentPage>=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
if((currentPage-1)>0)
{
hasPreviousPage=true;
}
else
{
hasPreviousPage=false;
}
SzEmployee[] employees=getSzEmployees();
this.description();
return employees;
}

public SzEmployee[] getLastpage(){

currentPage=totalPages;

if(currentPage>=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
if((currentPage-1)>0)
{
hasPreviousPage=true;
}
else
{
hasPreviousPage=false;
}
SzEmployee[] employees=getSzEmployees();
this.description();
return employees;
}

public SzEmployee[] getSzEmployees(){
// System.out.println("pageBean.getPros()開始執行;");


if(currentPage*pageRecorders<totalRows){//判斷是否爲最後一頁
pageEndRow=currentPage*pageRecorders;
pageStartRow=pageEndRow-pageRecorders;
}
else{
pageEndRow=totalRows;
pageStartRow=pageRecorders*(totalPages-1);
}
SzEmployee[] employees=new SzEmployee[pageEndRow-pageStartRow+1];

// System.out.println("pageStartRow="+pageStartRow);
// System.out.println("pageEndRow="+pageEndRow);
int j=0;
for(int i=pageStartRow;i<pageEndRow;i++)
{
if(i>=0){
SzEmployee employee=(SzEmployee)arrayList.get(i);
employees[j++]=employee;
}

}

this.description();
return employees;
}

public String toString(int temp)
{
String str=Integer.toString(temp);
return str;
}

public void description()
{

String description="共有數據數:"+this.getTotalRows()+

"共有頁數: "+this.getTotalPages() +

"當前頁數爲:"+this.getCurrentPage()+

" 是否有前一頁: "+this.isHasPreviousPage() +

" 是否有下一頁:"+this.isHasNextPage()+

" 開始行數:"+this.getPageStartRow()+

" 終止行數:"+this.getPageEndRow();

// System.out.println(description);

}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章