package com.wy.tool;//将该类保存到com.wy.tool包中
import java.util.ArrayList;//将java.util.ArrayList导入
import java.util.List;//将java.util.List导入
public class MyPagination {
public List<Object> list=null;//设置List类型的对象list
private int recordCount=0;//设置int类型变量recordCout
private int pagesize=0;//设置int类型变pagesize
private int maxPage=0;//设置int类型变量maxPage
//初始化分页信息
public List getInitPage(List list,int Page,int pagesize){
List<Object> newList=new ArrayList<Object>();//实例化List集合对象
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)//当循环i大于最大页码数量时,则程序中止
{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>();//实例化List集合对象
try{
for(int i=(Page-1)*pagesize;i<=Page*pagesize-1;i++){
try{
if(i>=recordCount){break;}//当循环i大于最大页码数量时,则程序中止
}catch(Exception e){}
newList.add((Object)list.get(i));//将查询的结果存放在list集合中
}
}catch(Exception e){
e.printStackTrace();
}
return newList;//返回指定页数的记录
}
//获取最大记录数
public int getMaxPage(){
int maxPage=(recordCount%pagesize==0)?(recordCount/pagesize):(recordCount/pagesize+1);//计算最大的记录数
return maxPage;//通过return关键字返回
}
//获取总记录数
public int getRecordSize(){
return recordCount;//通过return关键字返回记录总数
}
//获取当前页数
public int getPage(String str){
System.out.println("STR:"+str+"&&&&"+recordCount);
if(str==null){//当参数值为null,则将参数str赋值为0
str="0";
}
int Page=Integer.parseInt(str);//将参数类型进行转换,并赋值为page变量
if(Page<1){//当Page变量小于1时,则将变量赋值为1
Page=1;
}else{
if(((Page-1)*pagesize+1)>recordCount){
Page=maxPage;//将变量Page设置为最大页码数量
}
}
return Page;//通过return关键字返回当前页码数
}
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){//如果当前页码数大于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;//通过return关键字返回这个表格
}
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;
}
}
评论0
最新资源