万能分页方法!机会难得,在此送给大家

首先,页面写法:页面上一般有 四个按钮  上一页  首页 下一页  末页 或者 go【】页

直接给大家上代码,然后逐行讲解:

 

private Map<String, Object> getPaging(List list,int curPage){
        int Start=1;                                //起始
        int perPage=10;                                //每页多少条
        int total=0;                                 //总条数
        int End=0;                                    //结束
        int NumPage=0;                                //总页数
        total=list.size();
        if(total/perPage==0){
            NumPage=1;
        }else {
            NumPage=total/perPage;
            if(total%perPage>0){                    //证明有最后一页有不足10条的记录
                NumPage+=1;
            }
        }
        if(curPage==1){                                //第一页
            Start=0;
            End=10;
            if(End>total){                            //少于十条数据
                End=total;
                Start=0;
            }
        }else if(curPage==NumPage){                    //最后一页
            if(total%perPage==0){
                End=curPage*perPage;
                Start=End-perPage;
            }else{
            int c=total%perPage;                    //3
            int d=total/perPage;                    //6
            End=d*perPage+c;
            Start=d*perPage;
            }
        }else {                                        //上一页或下一页
            End=curPage*perPage;
            Start=End-perPage;
        }
        List<Object> ResultList=new ArrayList<Object>();
        for(int i=Start;i<End;i++){
            ResultList.add(list.get(i));
        }
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("curPage", curPage);                //当前页
        map.put("total", total);                    //总记录数
        map.put("perPage", perPage);                //每页多少条
        map.put("NumPage", NumPage);                //总页数
        map.put("ResultList", ResultList);            //分页后数据list
        return map;
    }

 

相信有些有经验的已经明白含义了。。在此给大家说一下,使用方法,首先,你要有一个list<自定义对象> 然后把它传给该方法,然后传递的是你的当前页,一把开始的时候的都是1。

先action里面调用该方法:

int curPage=Integer.parseInt(request.getParameter("curPage")==null?"1":request.getParameter("curPage")); //当前页

String pageFlag=request.getParameter("pageFlag")==null?"":request.getParameter("pageFlag");    //分页标识(点击上一页,下一页)

这俩个值,是你在jsp中传递过来的,当然前期是没有的。所以curPage在没获取到的时候值为1也就是默认显示第一页的数据

那么我们这么调用我们的分页方法呢?

Map<String, Object> map=getPaging(detailList,curPage);
   request.setAttribute("curPage", map.get("curPage"));          //当前页
   request.setAttribute("total", map.get("total"));              //总记录数
   request.setAttribute("perPage", map.get("perPage"));                //每页多少条
   request.setAttribute("intPageCount", map.get("NumPage"));      //总页数

这个detailList就是你查询出来的list 传递给分页方法即可

pageFlag 这个分页标识用什么用呢?为什么注释只写上一页或者下一页呢?

下面来看看一下他的使用,稍后为大家奉上jsp。

if("r".equals(pageFlag)){
    curPage=curPage-1;
   }
   if("n".equals(pageFlag)){
    curPage=curPage+1;
   }

这就是他的使用方法,计算curPage的值。

jsp代码,给大家奉上,大家就能更好的使用该分页方法了。废话不多说上代码

 

<s:if test="#request.DetailsList!=null&&#request.DetailsList.size()!=0">
                        <s:if test="#request.curPage1!=1">
                                <a href="DqYBCXAction!queryInfos.action?curPage1=1&name=<s:property value='#request.name' />&code=<s:property value='#request.code' />&pid=<s:property value='#request.pid' />&shebaohao=<s:property value='#request.shebaohao' /> ">首页</a>
                        </s:if>
                        <s:else><font style="color:#C0C0C0">首页</font></s:else>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <s:if test="#request.curPage1>1">
                                <a href="DqYBCXAction!queryInfos.action?pageFlag1=P1&curPage1=<s:property value='#request.curPage1' />&name=<s:property value='#request.name' />&code=<s:property value='#request.code' />&pid=<s:property value='#request.pid' />&shebaohao=<s:property value='#request.shebaohao' /> ">上一页</a>
                        </s:if>
                        <s:else><font style="color:#C0C0C0">上一页</font></s:else>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <s:if test="#request.curPage1<#request.intPageCount1">
                                <a href="DqYBCXAction!queryInfos.action?pageFlag1=N1&curPage1=<s:property value='#request.curPage1' />&name=<s:property value='#request.name' />&code=<s:property value='#request.code' />&pid=<s:property value='#request.pid' />&shebaohao=<s:property value='#request.shebaohao' /> " >下一页 </a>
                        </s:if>
                        <s:else><font style="color:#C0C0C0">下一页</font></s:else>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <s:if test="#request.curPage1!=#request.intPageCount1">
                                <a href="DqYBCXAction!queryInfos.action?curPage1=<s:property value='#request.intPageCount1' />&name=<s:property value='#request.name' />&code=<s:property value='#request.code' />&pid=<s:property value='#request.pid' />&shebaohao=<s:property value='#request.shebaohao' /> ">末页</a>
                        </s:if>
                        <s:else><font style="color:#C0C0C0">末页</font></s:else>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            当前页:<s:property value="#request.curPage1" />/<s:property value="#request.intPageCount1" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            </s:if>

 

 

jsp中已经对分页按钮进行了判断,传递值时也有详细写法,现在大家对该分页方法已经了解了吧?

如果还有什么不明白的请评论留言,看见时会给大家解释的。!

posted on 2013-11-22 14:53  谢皓宇  阅读(598)  评论(0编辑  收藏  举报

导航