jsp分页(例二)

字体大小: 中小 标准 ->行高大小: 标准

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+"]&nbsp;&nbsp;";
        try{
        if(Page>1){
            strHtml=strHtml+"<a href='?&Page=1'>第一页</a> ";
            strHtml=strHtml+"&nbsp;&nbsp;<a href='?Page="+(Page-1)+"'>上一页</a>";
        }
        if(Page<maxPage){
            strHtml=strHtml+"&nbsp;&nbsp;<a href='?Page="+(Page+1)+"'>下一页</a>&nbsp;&nbsp; <a href='?Page="+maxPage+"'>最后一页&nbsp;</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+"]&nbsp;&nbsp;";
        try{
        if(Page>1){
            strHtml=strHtml+"<a href='?"+method+"&Page=1'>第一页</a> ";
            strHtml=strHtml+"&nbsp;&nbsp;<a href='?"+method+"Page="+(Page-1)+"'>上一页</a>";
        }
        if(Page<maxPage){
            strHtml=strHtml+"&nbsp;&nbsp;<a href='?"+method+"Page="+(Page+1)+"'>下一页</a>&nbsp;&nbsp; <a href='?"+method+"Page="+maxPage+"'>最后一页&nbsp;</a>";
        }
        strHtml=strHtml+"</td> </tr>    </table>";
        }catch(Exception e){
            e.printStackTrace();
            
        }
        return strHtml;
    }
    
    
}
复制代码

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/69042.html