java web 分页技术总结

最近再做一个简易BBS,其中要用到分页技术,以前做过一个小型网上书店系统,记得当时是用弄了一个接口:PagingInterface,其中有得到总记录数、得到总页数、和一些根据当前页到结束页的查询方法。  然后再一个UserDAO类、BookDAO类中继承Paging方法,实现相应的“一些根据当前页到结束页的查询方法”等,然后再一个PagingServlet类中调用这些方法。。。。具体的类代码这里也不贴了,这种方法复用性不高,结构性差,不推荐使用。这次做这个BBS,经过几次的调试,终于弄成功了。下面是分页的主类:

[html]  view plain copy
  1. /**  
  2.  * 分页对象,进行一系列分页操作  
  3.  *   
  4.  * @author weiyiorng  
[html]  view plain copy
  1.  * @version 1.0 12/03/20  
  2.  * */  
  3. public class SplitPage {  
  4.     // 声明一些常量  
  5.     final public static String FIRSTPAGE = "first";// 请求的是首页  
  6.     final public static String PREVIOUSPAGE = "previous";// 请求上一页  
  7.     final public static String NEXTPAGE = "next";// 请求下一页  
  8.     final public static String LASTPAGE = "last";// 请求最后一页  
  9.   
  10.     // 声明一些变量  
  11.     private int pageRow = 3;// 每页显示记录数  
  12.     private int totalRow = 0;// 总的记录数,有数据库操作类DAO提供  
  13.     private int currentPage = 1;// 当前的页面  
  14.     private int firstPage = 1;// 首页位置  
  15.     private int totalPage = 1;// 总的页面数,默认为一页  
  16.   
  17.     public int getPageRow() {  
  18.         return pageRow;  
  19.     }  
  20.   
  21.     /**  
  22.      * 重新设置每页显示的记录数  
  23.      *   
  24.      * @param pageRow  
  25.      *            新的每页显示记录数  
  26.      */  
  27.     public void setPageRow(int pageRow) {  
  28.         if (pageRow == 0) {  
  29.             try {  
  30.                 throw new Exception();// 如果pageRow被设置为零,应当抛出异常.  
  31.             } catch (Exception e) {  
  32.                 // TODO Auto-generated catch block  
  33.                 e.printStackTrace();  
  34.             }  
  35.         }  
  36.   
  37.         this.pageRow = pageRow;// 修改每页的记录数  
  38.         this.totalPage = this.totalRow / this.pageRow  
  39.                 + ((this.totalRow % this.pageRow == 0) ? 0 : 1);  
  40.     }  
  41.   
  42.     public int getTotalRow() {  
  43.         return totalRow;  
  44.     }  
  45.   
  46.     public void setTotalRow(int totalRow) {// 设置分页对象的总记录属性后,就应该根据每页面显示记录数,计算得到总的页面数  
  47.         this.totalRow = totalRow;  
  48.         this.totalPage = this.totalRow/this.pageRow+((this.totalRow%this.pageRow==0)?0:1);  
  49.         System.out.println("当前页"+this.currentPage);  
  50.   
  51.     }  
  52.   
  53.     public int getCurrentPage() {  
  54.         return currentPage;  
  55.     }  
  56.   
  57.     public void setCurrentPage(int currentPage) {  
  58.         this.currentPage = currentPage;  
  59.     }  
  60.   
  61.     public int getFirstPage() {  
  62.         return firstPage;  
  63.     }  
  64.   
  65.     public void setFirstPage(int firstPage) {  
  66.         this.firstPage = firstPage;  
  67.     }  
  68.   
  69.     public int getTotalPage() {  
  70.         return totalPage;  
  71.     }  
  72.     //不应该提供方法设置总页面数,它是计算得到的   
  73.   
  74.     /**  
  75.      * 根据请求的标示符参数重新计算要现实的页面  
  76.      *   
  77.      * @param flag  
  78.      *            请求转向的页面标示符  
  79.      * @return int 返回新页  
  80.      */  
  81.     public int toNewPage(String flag) {  
  82.         int newPage = this.currentPage;  
  83.         if (flag != null && !"".equals(flag)) {  
  84.             if (SplitPage.FIRSTPAGE.equals(flag)) {  
  85.                 newPage = 1;  
  86.             } else if (SplitPage.LASTPAGE.equals(flag)) {  
  87.                 newPage = this.totalPage;  
  88.             } else if (SplitPage.NEXTPAGE.equals(flag)) {  
  89.                 newPage = this.currentPage  
  90.                         + ((this.currentPage == this.totalPage) ? 0 : 1);// 如果当前页面与总的页面数相等则不再向后(+1)  
  91.             } else if (SplitPage.PREVIOUSPAGE.equals(flag)) {  
  92.                 newPage = this.currentPage  
  93.                         - ((this.currentPage == this.firstPage) ? 0 : 1);// 如果当前页面与首页相等则不再向前(-1)  
  94.             } else {  
  95.                 // 传入的是个数字字符串参数  
  96.                 newPage = Integer.parseInt(flag.trim());  
  97.             }  
  98.         } else {// 请求的参数为空,则当前页码不变  
  99.             newPage = this.currentPage;  
  100.         }  
  101.         this.setCurrentPage(newPage);// 记得重新设置当期页面  
  102.         return newPage;  
  103.     }  
  104. }  

为什么说这个类重要呢?因为你在想要分页的时候,所有的分页参数都可以在这里面设置,动态静态的都可以实现,复用性很高!

下面声明一个分页接口,设置具体的分页对象(比如你要分页的是用户列表还是帖子列表)等:

[java]  view plain copy
  1. public interface PageInterface {  
  2.     /** 
  3.      * 查询所有的记录,调用分页生成器类中的分页方法查询数据 
  4.      *  
  5.      * @param splitPage 
  6.      *            分页对象 
  7.      * @return List<Object> 
  8.      * */  
  9.     public List<UserInfoVo> findUserAll(SplitPage splitPage);//查询用户列表  
  10.     /**  
  11.      * 查询所有的记录,调用分页生成器类中的分页方法查询数据  
  12.      *   
  13.      * @param splitPage  
[java]  view plain copy
  1.            *            分页对象  
  2.      * @return List<Object>  
  3.      * */  
  4.     public List<ForumInfoVo> findForumAll(SplitPage splitPage);//查询帖子  
  5.     /** 
  6.      * 提供总的记录数 
  7.      * */  
  8.   
  9.     public int getTotalRows();  
  10.     /** 
  11.      * 查询所有的记录,调用分页生成器类中的分页方法查询数据 
  12.      *  
  13.      * @param splitPage 
  14.      *            分页对象 
  15.      * @return List<Object> 
  16.      * */  
  17.     public List<ReforumInfoVo> findReforumAll(SplitPage splitPage);  
  18. }  


然后在各个DAO类(用户DAO、帖子DAO、RefoumDAO等)中实现此接口相应方法,注意在各个DAO类中应该有一个totalRow总记录数的获取和设置方法,得到指定记录数方法,简单起见,我这里是直接得到所有记录getAllRecord()(可以再分页显示时调用以设置分页类别,如查询某一个昵称的所有用户等)。

接下咱废话少说,直接上显示分页效果的代码:

forumList.jsp

[java]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsf/core"%>  
  3. <%@page import="com.weiyi.bbs.dao.ForumInfoDAO"%>  
  4. <%@page import="com.weiyi.bbs.util.*"%>  
  5. <%@page import="com.weiyi.bbs.domain.*"%>  
  6. <%  
  7.     String path = request.getContextPath();  
  8.     String basePath = request.getScheme() + "://"  
  9.             + request.getServerName() + ":" + request.getServerPort()  
  10.             + path + "/";  
  11. %>  
  12. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  13. <html>  
  14.     <head>  
  15.         <base href="<%=basePath%>">  
  16.   
  17.         <title>帖子列表</title>  
  18.   
  19.         <meta http-equiv="pragma" content="no-cache">  
  20.         <meta http-equiv="cache-control" content="no-cache">  
  21.         <meta http-equiv="expires" content="0">  
  22.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  23.         <meta http-equiv="description" content="This is my page">  
  24.         <script type="text/javascript" src="js/common.js" charset="utf-8"></script>  
  25.         <script type="text/javascript" charset="utf-8">  
  26.         function goPage(){  
  27.            var v = document.getElementById("selectPage").value;  
  28.            var u = document.getElementById("userId").value;  
  29.            window.location.href="forumList.jsp?flag="+v+"&userId="+u;  
  30.         }  
  31.         </script>  
  32.     </head>  
  33.     <!--spage分页对象,保存分页的详细信息 ,存在Session中,每次查询或者是分页数据时只要设置此对象的当前页 -->  
  34.     <!-- dao帖子操作类,主要用来获取总记录数-->  
  35.     <jsp:useBean id="spage" class="com.weiyi.bbs.util.SplitPage"  
  36.         scope="session"></jsp:useBean>  
  37.     <jsp:useBean id="dao" class="com.weiyi.bbs.dao.ForumInfoDAO"  
  38.         scope="session"></jsp:useBean>  
  39.     <%  
  40.         String flag = request.getParameter("flag");  
  41.         int newPage = spage.toNewPage(flag.trim());  
  42.         dao.setTotalRows(dao.getAllRecord());  
  43.         int totalRows = dao.getTotalRows();  
  44.         spage.setTotalRow(totalRows);  
  45.     %>  
  46.     <body>  
  47.         <div id="wrap">  
  48.             <h3>  
  49.                 查看帖子列表  
  50.             </h3>  
  51.             <input type="hidden" name="userId" id="userId" value="<%=request.getParameter("userId")%>"/>  
  52.             <a href="destroyservlet">退出</a>     
  53.             <a  
  54.                 href="forumcontroller?type=toNewForum&userId=<%=request.getParameter("userId")%>&power=user">发新帖</a>  
  55.             <table border="border">  
  56.                 <thead>  
  57.                     <tr>  
  58.                         <th>  
  59.                             ID  
  60.                         </th>  
  61.                         <th>  
  62.                             标题  
  63.                         </th>  
  64.                         <th>  
  65.                             发帖人  
  66.                         </th>  
  67.                         <th>  
  68.                             发帖时间  
  69.                         </th>  
  70.                         <th>  
  71.                             操作  
  72.                         </th>  
  73.                     </tr>  
  74.                 </thead>  
  75.                 <tbody>  
  76.                     <%  
  77.                         List<ForumInfoVo> list = dao.findForumAll(spage);  
  78.                         for (ForumInfoVo forum : list) {  
  79.                     %>  
  80.                     <tr>  
  81.                         <td><%=forum.getId()%></td>  
  82.                         <td><%=forum.getTitle()%></td>  
  83.                         <td><%=forum.getAuthorName()%></td>  
  84.                         <td><%=forum.getCreateTime()%></td>  
  85.                         <td>  
  86.                             <a  
  87.                                 href="forumcontroller?type=look&power=user&userId=<%=request.getParameter("userId")%>&id=<%=forum.getId()%>">查看</a>    
  88.                         </td>  
  89.                     </tr>  
  90.                     <%  
  91.                         }  
  92.                     %>  
  93.                 </tbody>  
  94.             </table>  
  95.             <div id="foot">  
  96.                 <a href="forumList.jsp?flag=<%=SplitPage.FIRSTPAGE%>&userId=<%=request.getParameter("userId")%>">首页</a>   
  97.                 <a href="forumList.jsp?flag=<%=SplitPage.PREVIOUSPAGE%>&userId=<%=request.getParameter("userId")%>">上一页</a>   
  98.                 <a href="forumList.jsp?flag=<%=SplitPage.NEXTPAGE%>&userId=<%=request.getParameter("userId")%>">下一页</a>   
  99.                 <a href="forumList.jsp?flag=<%=SplitPage.LASTPAGE%>&userId=<%=request.getParameter("userId")%>">末页</a>   
  100.                 <select id="selectPage" name="selectPage" οnchange="goPage();">  
  101.                     <%  
  102.                         for (int i = 1; i <= spage.getTotalPage(); ++i) {  
  103.                     %>  
  104.                     <option value="<%=i%>"  
  105.                         <%=(spage.getCurrentPage() == i) ? "selected='selected'"  
  106.                         : ""%> ><%=i%></option>  
  107.                         <%  
  108.                             }  
  109.                         %>  
  110.                       
  111.                 </select>  
  112.                 <label><%=spage.getCurrentPage()%>/<%=spage.getTotalPage()%>页  
  113.                 </label>  
  114.             </div>  
  115.         </div>  
  116.     </body>  
  117. </html>  


 

 运行效果:

有什么地方有不足之处,往各位同行多多指教哈!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值