首先我们要对页面需要的参数进行初始化,包括 每页行数 总行数 总页数 当前页,在jsp中插入如下代码
String classname="com.danale.content.Partnersinf"; CheckObject co = new CheckObject(); String select=request.getParameter("select"); String condition=request.getParameter("condition"); if(select=="")select=null; if(condition=="")condition=null; //以上用于搜索 int pagesize=10;//每页行数 int total=0;//总行数 if(select!=null&&condition!=null) { total=co.SerchObjectCount(classname,select,condition); }else { total=co.getObjectSize(classname); } //计算total 总行数 根据是不是搜索有不同 System.out.println("total="+total); int pagenum=(total+pagesize-1)/pagesize; if(pagenum<1) pagenum=1; //计算总页数 int pagenow=1;//当前页 if(request.getParameter("pagenow")!=null) { int n=Integer.parseInt(request.getParameter("pagenow")); if(1<=n&&n<=pagenum) pagenow=n; } List list=null; if(select!=null&&condition!=null)//根据是不是搜索要得到的结果有所不同。 { list=co.SerchObject(classname,(pagenow-1)*pagesize,pagesize,select,condition); } else{ list=co.getObject(classname,(pagenow-1)*pagesize,pagesize); }
底部页码:
<ul class="page_info">
<%
int pre=pagenow-1;
int next=pagenow+1;
if(pre<1)pre=1;
if(next>pagenum)next=pagenow;
%>
<li><a href="?pagenow=1<%if(select!=null&&condition!=null)out.print("&select="+select+"&condition="+condition);%>"><img src="images/arrow_leftend_off.png"></img></a></li>
<li><a href="?pagenow=<%=pre%><%if(select!=null&&condition!=null)out.print("&select="+select+"&condition="+condition);%>"> <img src="images/arrow_left_off.png"></img></a></li>
<%
int begain=0;
int end=0;
if(pagenum<=5)//页数不足5
{
begain=1;
end=pagenum;
}
else//页数大于5
{
if(pagenow>=3&&pagenow<=pagenum-3)
{
begain=pagenow-2;
end=pagenow+2;
}
else if(pagenow<3)
{
begain=1;
end=5;
}else if(pagenow>pagenum-3)
{
begain=pagenum-4;
end=pagenum;
}
}
for(int i=begain;i<=end;i++)
{
%><li class="page_num"><a <%if(i==pagenow)out.print("style=‘color:red;‘");%> href="?pagenow=<%=i%><%if(select!=null&&condition!=null)out.print("&select="+select+"&condition="+condition);%>"><%=i%></a></li>
<%
}
%>
<li><a href="?pagenow=<%=next%><%if(select!=null&&condition!=null)out.print("&select="+select+"&condition="+condition);%>"><img src="images/arrow_right_off.png"/></a></li>
<li><a href="?pagenow=<%=pagenum%><%if(select!=null&&condition!=null)out.print("&select="+select+"&condition="+condition);%>"><img src="images/arrow_rightend_off.png"/></a></li>
</ul>