看到大家对struts的学习热情很高,而且这方面的资料很少,看着大家很累,所以发发善心,看到很好的一种分页方法,就贴出来大家共同分享!(原作在javaworld),有不完善的地方,希望大家改正!!!
使用一个页面控制类,它记录页面信息,如上页,下页,当前页等。在查询的Action中,将这个控制类和查询条件一起传递给数据库访问bean,然后将这两个参数保存在用户session中。在分页控制Action中,利用接收到的分页参数调用数据库访问的bean.
(1)分页控制类
package com.tower.util;
public class PageController {
int totalRowsAmount; file://总行数
boolean rowsAmountSet; file://是否设置过totalRowsAmount
int pageSize=2; file://每页行数
int currentPage=1; file://当前页码
int nextPage;
int previousPage;
int totalPages; file://总页数
boolean hasNext; file://是否有下一页
boolean hasPrevious; file://是否有前一页
String description;
int pageStartRow;
int pageEndRow;
public PageController(int totalRows){
setTotalRowsAmount(totalRows);
}
public PageController(){}
public void setTotalRowsAmount(int i) {
if(!this.rowsAmountSet){
totalRowsAmount = i;
totalPages=totalRowsAmount/pageSize+1;
setCurrentPage(1);
this.rowsAmountSet=true;
}
}
public void setCurrentPage(int i) {
currentPage = i;
nextPage=currentPage+1;
previousPage=currentPage-1;
file://计算当前页开始行和结束行
if(currentPage*pageSize<totalRowsAmount){
pageEndRow=currentPage*pageSize;
pageStartRow=pageEndRow-pageSize+1;
}else{
pageEndRow=totalRowsAmount;
pageStartRow=pageSize*(totalPages-1)+1;
}
if (nextPage>totalPages){
hasNext=false;
}else{
hasNext=true;
}
if(previousPage==0){
hasPrevious=false;
}else{
hasPrevious=true;
};
System.out.println(this.description());
}
public int getCurrentPage() {
return currentPage;
}
public boolean isHasNext() {
return hasNext;
}
public boolean isHasPrevious() {
return hasPrevious;
}
public int getNextPage() {
return nextPage;
}
public int getPageSize() {
return pageSize;
}
public int getPreviousPage() {
return previousPage;
}
public int getTotalPages() {
return totalPages;
}
public int getTotalRowsAmount() {
return totalRowsAmount;
}
public void setHasNext(boolean b) {
hasNext = b;
}
public void setHasPrevious(boolean b) {
hasPrevious = b;
}
public void setNextPage(int i) {
nextPage = i;
}
public void setPageSize(int i) {
pageSize = i;
}
public void setPreviousPage(int i) {
previousPage = i;
}
public void setTotalPages(int i) {
totalPages = i;
}
public int getPageEndRow() {
return pageEndRow;
}
public int getPageStartRow() {
return pageStartRow;
}
public String getDescription(){
String description="Total:"+this.getTotalRowsAmount()+
" items "+this.getTotalPages() +" pages";
// this.currentPage+" Previous "+this.hasPrevious +
// " Next:"+this.hasNext+
// " start row:"+this.pageStartRow+
// " end row:"+this.pageEndRow;
return description;
}
public String description(){
String description="Total:"+this.getTotalRowsAmount()+
" items "+this.getTotalPages() +" pages,Current page:"+
this.currentPage+" Previous "+this.hasPrevious +
" Next:"+this.hasNext+
" start row:"+this.pageStartRow+
" end row:"+this.pageEndRow;
return description;
}
public static void main(String args[]){
PageController pc=new PageController(3);
System.out.println(pc.getDescription());
// pc.setCurrentPage(2);
// System.out.println(pc.description());
// pc.setCurrentPage(3);
// System.out.println(pc.description());
}