jsp页面:
<jsp:useBean id="pagination" class="com.wy.tool.MyPagination" scope="session"></jsp:useBean>
<% String str=request.getParameter("Page"); int Page=1; List list=null; if(str==null){ list=(List)request.getAttribute("list"); int pagesize=15; //指定每页显示的记录数 list=pagination.getInitPage(list,Page,pagesize); //初始化分页信息 }else{ Page=pagination.getPage(str); list=pagination.getAppointPage(Page); //获取指定页的数据 } %>
<%=pagination.printCtrl(Page,"method=queryTrain")%>
MyPagination.java
package com.wy.tool; import java.util.ArrayList; import java.util.List; public class MyPagination { public List<Object> list=null; private int recordCount=0; private int pagesize=0; private int maxPage=0; //初始化分页信息 public List getInitPage(List list,int Page,int pagesize){ List<Object> newList=new ArrayList<Object>(); this.list=list; recordCount=list.size(); this.pagesize=pagesize; this.maxPage=getMaxPage(); try{ for(int i=(Page-1)*pagesize;i<=Page*pagesize-1;i++){ try{ if(i>=recordCount){break;} }catch(Exception e){} newList.add((Object)list.get(i)); } }catch(Exception e){ e.printStackTrace(); } return newList; } //获取指定页的数据 public List<Object> getAppointPage(int Page){ List<Object> newList=new ArrayList<Object>(); try{ for(int i=(Page-1)*pagesize;i<=Page*pagesize-1;i++){ try{ if(i>=recordCount){break;} }catch(Exception e){} newList.add((Object)list.get(i)); } }catch(Exception e){ e.printStackTrace(); } return newList; } //获取最大记录数 public int getMaxPage(){ int maxPage=(recordCount%pagesize==0)?(recordCount/pagesize):(recordCount/pagesize+1); return maxPage; } //获取总记录数 public int getRecordSize(){ return recordCount; } //获取当前页数 public int getPage(String str){ System.out.println("STR:"+str+"&&&&"+recordCount); if(str==null){ str="0"; } int Page=Integer.parseInt(str); if(Page<1){ Page=1; }else{ if(((Page-1)*pagesize+1)>recordCount){ Page=maxPage; } } return Page; } public String printCtrl(int Page){ String strHtml="<table width='370' border='0' cellspacing='0' cellpadding='0'><tr> <td height='24' align='right'>当前页数:["+Page+"/"+maxPage+"] "; try{ if(Page>1){ strHtml=strHtml+"<a href='?&Page=1'>第一页</a> "; strHtml=strHtml+" <a href='?Page="+(Page-1)+"'>上一页</a>"; } if(Page<maxPage){ strHtml=strHtml+" <a href='?Page="+(Page+1)+"'>下一页</a> <a href='?Page="+maxPage+"'>最后一页 </a>"; } strHtml=strHtml+"</td> </tr> </table>"; }catch(Exception e){ e.printStackTrace(); } return strHtml; } public String printCtrl(int Page,String method){ method=method+"&"; String strHtml="<table width='370' border='0' cellspacing='0' cellpadding='0'><tr> <td height='24' align='right'>当前页数:["+Page+"/"+maxPage+"] "; try{ if(Page>1){ strHtml=strHtml+"<a href='?"+method+"&Page=1'>第一页</a> "; strHtml=strHtml+" <a href='?"+method+"Page="+(Page-1)+"'>上一页</a>"; } if(Page<maxPage){ strHtml=strHtml+" <a href='?"+method+"Page="+(Page+1)+"'>下一页</a> <a href='?"+method+"Page="+maxPage+"'>最后一页 </a>"; } strHtml=strHtml+"</td> </tr> </table>"; }catch(Exception e){ e.printStackTrace(); } return strHtml; } }