我手上的CMS项目是JSP+Struts2做的,DAO直接是JDBC,这样倒也清晰,弄明白以后再往s2sh上移植吧,最后是jeecms。
一:分页类的实现:(相当于DAOImpl)
计算总页数时候:如果能整除最好,不能整除的项目要显示在最后一页,所以要加1
显示分页导航信息时:如果现在是最后一页的话,就不显示“下一页”和“尾页”了
public class CreatePage {
private int CurrentP; //当前页码
private int AllP; //总页数
private int AllR; //总记录数
private int PerR; //每页显示记录数
private String PageLink; //分页导航栏信息
private String PageInfo; //分页状态显示信息
public CreatePage(){
CurrentP=1;
AllP=1;
AllR=0;
PerR=3;
PageLink="";
PageInfo="";
}
/** 设置每页显示记录数 */
public void setPerR(int PerR){
this.PerR=PerR;
}
/** 设置总记录数 */
public void setAllR(int AllR){
this.AllR=AllR;
}
/** 计算总页数 */
public void setAllP(){
AllP=(AllR%PerR==0)?(AllR/PerR):(AllR/PerR+1);//如果能整除最好,不能整除的项目要显示在最后一页,所以要加1
}
/** 设置当前页码 */
public void setCurrentP(String currentP) {
if(currentP==null||currentP.equals(""))//预防
currentP="1";
try{
CurrentP=Integer.parseInt(currentP);
}catch(NumberFormatException e){
CurrentP=1;
e.printStackTrace();
}
if(CurrentP<1)
CurrentP=1;
if(CurrentP>AllP)
CurrentP=AllP;
}
/** 设置分页状态显示信息 */
public void setPageInfo(){
if(AllP>1){//用于输出分页显示的提示信息
PageInfo="<table border='0' cellpadding='3'><tr><td>";
PageInfo+="每页显示:"+PerR+"/"+AllR+" 条记录!";
PageInfo+="当前页:"+CurrentP+"/"+AllP+" 页!";
PageInfo+="</td></tr></table>";
}
}
/** 设置分页导航栏信息 */
public void setPageLink(String gowhich){
if(gowhich==null)
gowhich="";
if(gowhich.indexOf("?")>=0)//如果字串里有问号,要补参数
gowhich+="&";
else
gowhich+="?";//没问号的话,先补问号
if(AllP>1){
PageLink="<table border='0' cellpadding='3'><tr><td>";
if(CurrentP>1){
PageLink+="<a href='"+gowhich+"showpage=1'>首页</a>";
PageLink+="<a href='"+gowhich+"showpage="+(CurrentP-1)+"'>上一页</a>";
}
if(CurrentP<AllP){//如果现在是最后一页的话,就不显示“下一页”和“尾页”了
PageLink+="<a href='"+gowhich+"showpage="+(CurrentP+1)+"'>下一页</a>";
PageLink+="<a href='"+gowhich+"showpage="+AllP+"'>尾页</a>";
}
PageLink+="</td></tr></table>";
}
}
二:DAO层的分页调用
public CreatePage OpCreatePage(String sqlall,Object[] params,int perR,String strCurrentP,String gowhich){
CreatePage page=new CreatePage();//创建上述分页类
page.setPerR(perR);
if(sqlall!=null&&!sqlall.equals("")){
DB mydb=new DB();//这儿的DB相当于DAOImpl
mydb.doPstm(sqlall,params); // 是CRUD操作接口
try {
ResultSet rs=mydb.getRs(); //获得结果集
if(rs!=null&&rs.next()){
rs.last();
page.setAllR(rs.getRow()); //获得总记录数
page.setAllP();
page.setCurrentP(strCurrentP);
page.setPageInfo();
page.setPageLink(gowhich); //目标分页数
rs.close();
}
} catch (SQLException e) {
System.out.println("OpDB.java/OpCreatePage()方法:创建CreatePage分页类失败!");
e.printStackTrace();
}finally{
mydb.closed(); //因为是JDBC操作的,所以要关一下
}
}
return page;
}
这样service层调用Dao层的OpCreatePage(String sqlall,Object[] params,int perR,String strCurrentP,String gowhich)方法,把sql、sql语句中的占位参数、每页显示行数、当前分页页码、跳转页码传递进来,DaoImpl去执行
三:jsp显示
显示出组装好的分页信息
<%@ taglib uri="/struts-tags" prefix="s2"%>
<html>
<head><title>分页导航栏</title></head>
<body>
<table border="0" width="100%" cellspacing="0">
<tr>
<td width="60%"><s2:property escape="false" value="#request.createpage.PageInfo"/></td>
<td align="center" width="40%"><s2:property escape="false" value="#request.createpage.PageLink"/></td>
</tr>
</table>
</body>
</html>
需要显示分页信息的时候,这个jsp被其他页面include