这个是pagebean.java文件package page;
import bean.Book;
import java.util.*;
import javax.servlet.*;public class PageBean { int currentPage=1; //当前页
public int totalPages=0; //总页数
int recorders=5;//每页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();
it=arrayList.iterator();
hasPreviousPage=false;
currentPage=1;
if((totalRows%recorders)==0)
{
totalPages=totalRows/recorders;
}
else
{
totalPages=totalRows/recorders+1;
} if(currentPage>=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
if(totalRows<recorders)
{
this.pageStartRow=0;
this.pageEndRow=totalRows;
}
else
{
this.pageStartRow=0;
this.pageEndRow=recorders;
}} /**
* @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 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 Book[] 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()");
Book[] books=getBooks();
this.description(); return books;
}public Book[] 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;
}
Book[] books=getBooks();
this.description();
return books;
}public Book[] getBooks(){
System.out.println("pageBean.getBooks()开始执行;");
if((totalRows%recorders)==0)
{
totalPages=totalRows/recorders;
}
else
{
totalPages=totalRows/recorders+1;
}
if(currentPage*recorders<totalRows){//判断是否为最后一页
pageEndRow=currentPage*recorders;
pageStartRow=pageEndRow-recorders;
}
else{
pageEndRow=totalRows;
pageStartRow=recorders*(totalPages-1);
}
Book[] books=new Book[pageEndRow-pageStartRow+1]; System.out.println("pageStartRow="+pageStartRow);
System.out.println("pageEndRow="+pageEndRow);
int j=0;
for(int i=pageStartRow;i<pageEndRow;i++)
{ Book book=(Book)arrayList.get(i);
books[j++]=book; }
System.out.println("要显示的页面数据已经封装,具体信息如下:");
this.description();
return books;
}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);}
public String getRecorders() {
return toString(recorders);
}
public void setRecorders(int recorders) {
this.recorders = recorders;
}}
import bean.Book;
import java.util.*;
import javax.servlet.*;public class PageBean { int currentPage=1; //当前页
public int totalPages=0; //总页数
int recorders=5;//每页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();
it=arrayList.iterator();
hasPreviousPage=false;
currentPage=1;
if((totalRows%recorders)==0)
{
totalPages=totalRows/recorders;
}
else
{
totalPages=totalRows/recorders+1;
} if(currentPage>=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
if(totalRows<recorders)
{
this.pageStartRow=0;
this.pageEndRow=totalRows;
}
else
{
this.pageStartRow=0;
this.pageEndRow=recorders;
}} /**
* @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 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 Book[] 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()");
Book[] books=getBooks();
this.description(); return books;
}public Book[] 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;
}
Book[] books=getBooks();
this.description();
return books;
}public Book[] getBooks(){
System.out.println("pageBean.getBooks()开始执行;");
if((totalRows%recorders)==0)
{
totalPages=totalRows/recorders;
}
else
{
totalPages=totalRows/recorders+1;
}
if(currentPage*recorders<totalRows){//判断是否为最后一页
pageEndRow=currentPage*recorders;
pageStartRow=pageEndRow-recorders;
}
else{
pageEndRow=totalRows;
pageStartRow=recorders*(totalPages-1);
}
Book[] books=new Book[pageEndRow-pageStartRow+1]; System.out.println("pageStartRow="+pageStartRow);
System.out.println("pageEndRow="+pageEndRow);
int j=0;
for(int i=pageStartRow;i<pageEndRow;i++)
{ Book book=(Book)arrayList.get(i);
books[j++]=book; }
System.out.println("要显示的页面数据已经封装,具体信息如下:");
this.description();
return books;
}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);}
public String getRecorders() {
return toString(recorders);
}
public void setRecorders(int recorders) {
this.recorders = recorders;
}}
解决方案 »
- 求好心的大神。。。
- 使用Hibernate3出现的问题
- Struts2 <s:checkboxlist/> 标签的问题,高手帮忙呀!
- j2ee 中点击复选框全部删除问题
- Spring+Hibernate+Struts, Spring管理下的Bean,为啥被构造了四次?!!!!!
- JSP页面只显示部分
- tapestry如何实现include机制
- apache+jdk+resin配置的jsp环境,连接数据库成功,不过浏览器的标题栏出现???JSP????MYSQL?????,这是怎么一回事?
- axis 附件问题 请大家帮忙找错 100分相赠 不够再倾囊相送
- jsp问题: The server encountered an internal error () that prevented it from fulfil
- 来者有分,hibernate+mssqlserver+eclipse 为什么jdbc驱动程序不好使呢?
- 去表单数据的中文乱码问题??????
import org.apache.struts.action.*;
import javax.servlet.http.*;
import comm.Constants;
import javax.servlet.*;import bean.Book;
import java.util.*;
import javax.sql.DataSource;
import java.sql.*;
public class PageListAction extends Action { public PageListAction(){}
ArrayList arrayList=new ArrayList();
PageBean pb; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String action;
action=request.getParameter("action");
String pageRecorders = request.getParameter("Recorder");if(action==null || action.equals("null")){ //第一次读取数据
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:eBookStore");
arrayList=Book.getAllBook(con);
System.out.println("第一步,数据已经成功传递到Action,action="+action);
}catch(Exception e){
e.printStackTrace();
System.out.println("数据库连接出现异常");
} pb=new PageBean(arrayList);
if(pageRecorders!=null){
int p = Integer .parseInt(pageRecorders);
pb.setRecorders(p);
} Book[] books=pb.getBooks();
pb.description();
request.setAttribute("result",books);
request.setAttribute("page",pb); }
else
{
if(action=="nextPage" || action.equals("nextPage"))
{
System.out.println("参数action="+action);
System.out.println("函数pb.getNextPage()准备执行");
Book[]books=pb.getNextPage();
request.setAttribute("page",pb);
request.setAttribute("result",books);
}
if(action=="previousPage" || action.equals("previousPage"))
{
System.out.println("参数action="+action);
System.out.println("函数pb.getPreviouspage()准备执行");
Book[] books=pb.getPreviouspage();
request.setAttribute("page",pb);
request.setAttribute("result",books); }
}
return (mapping.findForward("success"));
}
}
--DATAGRID