一个非常强大的SSH分页方法

分页基本上是每个项目都会用到的模块。在这里我总结了一下网上主流的分页模式,自己做了一套。跟大家分享下

这个分页方法可以完成对任意表,任意查询条件的查询和分页。基本上可以代替项目中所有的数据库查询功能,当然只限于简单逻辑上的。初步整理,有什么不妥的地方还望大家指出、探讨。

首先是分页实体类:

  1. package  engine.entity;  
  2.   
  3. import  java.util.List;  
  4.   
  5. public   class  Pager {  
  6.     /**  
  7.      *  是否有上一页   
  8.      */   
  9.     private   boolean  hasPrePage;  
  10.     /**  
  11.      * 是否有下一页   
  12.      */   
  13.     private   boolean  hasNextPage;  
  14.     /**  
  15.      *  每页的数量   
  16.      */   
  17.     private   int  everyPage;  
  18.     /**  
  19.      *  总页数  
  20.      */   
  21.     private   int  totalPage;  
  22.     /**  
  23.      * 当前页   
  24.      */   
  25.     private   int  currentPage;   
  26.     /**  
  27.      *  起始点   
  28.      */   
  29.     private   int  beginIndex;   
  30.     /**  
  31.      *  总记录数  
  32.      */   
  33.     private   int  totalCount;   
  34.     /**  
  35.      * 该页名称  
  36.      */   
  37.     private  String pageName;   
  38.     /**  
  39.      * 查询条件  
  40.      */   
  41.     private  String conString;  
  42.     public  String getConString() {  
  43.         return  conString;  
  44.     }  
  45.     /**  
  46.      * 设置查询条件  
  47.      */   
  48.     public   void  setConString(String conditionString) {  
  49.         this .conString = conditionString;  
  50.     }  
  51.     /**  
  52.      * 设置该页名称(即查询表名)  
  53.      */   
  54.     public  String getPageName() {  
  55.         return  pageName;  
  56.     }  
  57.   
  58.     public   void  setPageName(String tableName) {  
  59.         this .pageName = tableName;  
  60.     }  
  61.   
  62.     public   int  getTotalCount() {  
  63.         return  totalCount;  
  64.     }  
  65.     /**  
  66.      *  设置总记录数  
  67.      */   
  68.     public   void  setTotalCount( int  totalCount) {  
  69.         this .totalCount = totalCount;  
  70.     }  
  71.     // construct the page by everyPage    
  72.     public  Pager( int  everyPage){  
  73.         this .everyPage = everyPage;  
  74.     }  
  75.     //The whole constructor    
  76.     public  Pager( boolean  hasPrePage,  boolean  hasNextPage,   
  77.                     int  everyPage,  int  totalPage,  
  78.                     int  currentPage,  int  beginIndex, int  totalCount,  
  79.                     String pageName,String conString) {  
  80.         this .hasPrePage = hasPrePage;  
  81.         this .hasNextPage = hasNextPage;  
  82.         this .everyPage = everyPage;  
  83.         this .totalPage = totalPage;  
  84.         this .currentPage = currentPage;  
  85.         this .beginIndex = beginIndex;  
  86.         this .totalCount = totalCount;  
  87.         this .pageName = pageName;  
  88.         this .conString = conString;  
  89.     }  
  90.     public   int  getBeginIndex() {  
  91.         return  beginIndex;  
  92.     }  
  93.     /**  
  94.      *  设置起始点   
  95.      */   
  96.     public   void  setBeginIndex( int  beginIndex) {  
  97.         this .beginIndex = beginIndex;  
  98.     }  
  99.     public   int  getCurrentPage() {  
  100.         return  currentPage;  
  101.     }  
  102.     /**  
  103.      * 设置当前页   
  104.      */   
  105.     public   void  setCurrentPage( int  currentPage) {  
  106.         this .currentPage = currentPage;  
  107.     }  
  108.     public   int  getEveryPage() {  
  109.         return  everyPage;  
  110.     }  
  111.     /**  
  112.      *  设置每页的数量   
  113.      */   
  114.     public   void  setEveryPage( int  everyPage) {  
  115.         this .everyPage = everyPage;  
  116.     }  
  117.     /**  
  118.      * 是否有下一页   
  119.      */   
  120.     public   boolean  getHasNextPage() {  
  121.         return  hasNextPage;  
  122.     }  
  123.     public   void  setHasNextPage( boolean  hasNextPage) {  
  124.         this .hasNextPage = hasNextPage;  
  125.     }  
  126.     /**  
  127.      * 是否有上一页   
  128.      */   
  129.     public   boolean  getHasPrePage() {  
  130.         return  hasPrePage;  
  131.     }  
  132.     public   void  setHasPrePage( boolean  hasPrePage) {  
  133.         this .hasPrePage = hasPrePage;  
  134.     }  
  135.     public   int  getTotalPage() {  
  136.         return  totalPage;  
  137.     }  
  138.     /**  
  139.      *  设置总页数  
  140.      */   
  141.     public   void  setTotalPage( int  totalPage) {  
  142.         this .totalPage = totalPage;  
  143.     }  
  144.   
  145. }  

然后创建分页结果集(将分页结果打包,便于访问):

  1. package  engine.entity;  
  2.   
  3. import  java.util.List;  
  4.   
  5. public   class  Result {  
  6.     private  Pager pager;    
  7.     private  List content;    
  8.   
  9.          //The default constructor    
  10.         public  Result() {  
  11.             super ();  
  12.         }   
  13.          /**  
  14.           * @param 分页信息  
  15.           * @param 每页显示的集合  
  16.           */   
  17.         public  Result(Pager pager, List content) {  
  18.             this .pager = pager;  
  19.             this .content = content;  
  20.         }  
  21.   
  22.          
  23.         public  List getContent() {  
  24.             return  content;  
  25.         }  
  26.   
  27.          
  28.         public  Pager getPager() {  
  29.             return  pager;  
  30.         }  
  31.   
  32.          
  33.         public   void  setContent(List content) {  
  34.             this .content = content;  
  35.         }  
  36.   
  37.          
  38.         public   void  setPager(Pager pager) {  
  39.             this .pager = pager;  
  40.         }  
  41. }  

构建一个page的工厂PageUtil(处理分页相关计算):

  1. package  engine;  
  2.   
  3. import  engine.entity.Pager;  
  4.   
  5. public   class  PagerUtil {  
  6.     // Use the origin page to create a new page   
  7.   
  8.   
  9.     public   static  Pager createPage(Pager page,  int  totalRecords) {  
  10.         return  createPage(page.getEveryPage(), page.getCurrentPage(),page.getPageName(),  
  11.                 page.getConString(),totalRecords);  
  12.     }  
  13.   
  14.   
  15.     //the basic page utils not including exception handler   
  16.   
  17.   
  18.     public   static  Pager createPage( int  everyPage,  int  currentPage,  
  19.             String pageName,String conString,int  totalRecords) {  
  20.         everyPage = getEveryPage(everyPage);  
  21.         currentPage = getCurrentPage(currentPage);  
  22.         int  beginIndex = getBeginIndex(everyPage, currentPage);  
  23.         int  totalPage = getTotalPage(everyPage, totalRecords);  
  24.         boolean  hasNextPage = hasNextPage(currentPage, totalPage);  
  25.         boolean  hasPrePage = hasPrePage(currentPage);  
  26.         System.out.println(everyPage+"*" +totalPage+ "*" +  
  27.                 currentPage+"*" +beginIndex+ "*" +totalRecords);  
  28.         return   new  Pager(hasPrePage, hasNextPage, everyPage, totalPage,  
  29.                 currentPage, beginIndex, totalRecords, pageName, conString);  
  30.     }  
  31.   
  32.     private   static   int  getEveryPage( int  everyPage) {  
  33.         return  everyPage ==  0  ?  10  : everyPage;  
  34.     }  
  35.   
  36.     private   static   int  getCurrentPage( int  currentPage) {  
  37.         return  currentPage ==  0  ?  1  : currentPage;  
  38.     }  
  39.   
  40.     private   static   int  getBeginIndex( int  everyPage,  int  currentPage) {  
  41.         return  (currentPage -  1 ) * everyPage;  
  42.     }  
  43.   
  44.     private   static   int  getTotalPage( int  everyPage,  int  totalRecords) {  
  45.         int  totalPage =  0 ;  
  46.       
  47.         if  (totalRecords % everyPage ==  0 )  
  48.             totalPage = totalRecords / everyPage;  
  49.         else   
  50.             totalPage = totalRecords / everyPage + 1 ;  
  51.       
  52.         return  totalPage;  
  53.     }  
  54.   
  55.     private   static   boolean  hasPrePage( int  currentPage) {  
  56.         return  currentPage ==  1  ?  false  :  true ;  
  57.     }  
  58.   
  59.     private   static   boolean  hasNextPage( int  currentPage,  int  totalPage) {  
  60.         return  currentPage == totalPage || totalPage ==  0  ?  false  :  true ;  
  61.     }  
  62.   
  63. }  

数据访问层接口:

  1. package  hibernate.dao;  
  2.   
  3. import  java.util.List;  
  4.   
  5. import  engine.entity.Pager;  
  6.   
  7. public   interface  PagerProductDAO {  
  8.     public   void  setPager(Pager pager);  
  9.     /**  
  10.      * @return 分页后的数据  
  11.      */   
  12.     public  List getProductByPage();   
  13.     /**  
  14.      * 不使用分页  
  15.      * @return 所有符合条件的数据  
  16.      */   
  17.     public  List getProducts();  
  18.     /**  
  19.      * @return 数据的总数  
  20.      */   
  21.     public   int  getProductCount();    
  22. }  

数据访问层接口实现:

  1. package  hibernate.dao;  
  2.   
  3. import  java.sql.SQLException;  
  4. import  java.util.List;  
  5.   
  6. import  org.hibernate.HibernateException;  
  7. import  org.hibernate.Query;  
  8. import  org.hibernate.Session;  
  9. import  org.springframework.orm.hibernate3.HibernateCallback;  
  10. import  org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  11.   
  12. import  engine.entity.Pager;  
  13.   
  14.   
  15. public   class  PagerProductDAOImpl  extends  HibernateDaoSupport  implements  PagerProductDAO {  
  16.   
  17.     Pager pager;  
  18.     public  Pager getPager() {  
  19.         return  pager;  
  20.     }  
  21.     public   void  setPager(Pager page) {  
  22.         this .pager = page;  
  23.     }  
  24.     public  List getProductByPage() {  
  25.         // TODO Auto-generated method stub   
  26.         return  getHibernateTemplate().executeFind( new  HibernateCallback(){  
  27.             public  Object doInHibernate(Session session)  throws  HibernateException, SQLException {  
  28.                 Query query=session.createQuery("from " +pager.getPageName()+ " where " +pager.getConString());  
  29.                 query.setFirstResult(pager.getBeginIndex()); //hibernate分页的精髓   
  30.                 query.setMaxResults(pager.getEveryPage());  
  31.                 return  query.list();  
  32.             }  
  33.             });  
  34.     }  
  35.     public  List getProducts(){  
  36.         return  getHibernateTemplate().find( "from " +pager.getPageName()+ " where " +pager.getConString());  
  37.           
  38.     }  
  39.     public   int  getProductCount() {  
  40.         // TODO Auto-generated method stub   
  41.         List list=getHibernateTemplate().find("from " +pager.getPageName()+ " where " +pager.getConString());  
  42.         return  ((Integer)list.size()).intValue();  
  43.   
  44.     }  
  45.   
  46. }  

业务层接口:

  1. package  engine;  
  2.   
  3. import  engine.entity.Pager;  
  4. import  engine.entity.Result;  
  5.   
  6. public   interface  PagerProduct {  
  7.     public  Result listProduct(Pager pager);  
  8. }  

业务层接口实现:

  1. package  engine.impl;  
  2.   
  3. import  java.util.List;  
  4.   
  5. import  engine.PagerProduct;  
  6. import  engine.PagerUtil;  
  7. import  engine.entity.Pager;  
  8. import  engine.entity.Result;  
  9. import  hibernate.dao.PagerProductDAO;  
  10.   
  11. public   class  PagerProductImpl  implements  PagerProduct {  
  12.   
  13.     private  PagerProductDAO pagerProductDAO;  
  14.     public  PagerProductDAO getPagerProductDAO() {  
  15.         return  pagerProductDAO;  
  16.     }  
  17.     public   void  setPagerProductDAO(PagerProductDAO pagerProductDAO) {  
  18.         this .pagerProductDAO = pagerProductDAO;  
  19.     }  
  20.     public  Result listProduct(Pager pager) {  
  21.         // TODO Auto-generated method stub   
  22.         this .pagerProductDAO.setPager(pager);  
  23.         List products=null ;  
  24.         if (pager.getEveryPage()== 0 ){  
  25.             //不使用分页   
  26.             products = this .pagerProductDAO.getProducts();  
  27.         }  
  28.         else {  
  29.             int  totalRecords =  this .pagerProductDAO.getProductCount();  
  30.             pager = PagerUtil.createPage(pager, totalRecords);  
  31.             //载入新生产的page   
  32.             this .pagerProductDAO.setPager(pager);  
  33.             products = this .pagerProductDAO.getProductByPage();       
  34.         }  
  35.         return   new  Result(pager, products);  
  36.     }  
  37.       
  38. }  

呼~~终于到productAction啦

  1. public   class  PlaceAction extends DispatchAction {  
  2.       
  3.     private  PagerProduct pagerProduct;  
  4.     public  PagerProduct getPagerProduct() {  
  5.         return  pagerProduct;  
  6.     }  
  7.   
  8.     public   void  setPagerProduct(PagerProduct pagerProduct) {  
  9.         this .pagerProduct = pagerProduct;  
  10.     }  
  11.   
  12.     public  ActionForward find(ActionMapping mapping, ActionForm form,  
  13.             HttpServletRequest request, HttpServletResponse response)  
  14.             throws Exception {  
  15.         Pager pager=new  Pager(10); //设置每页10条记录。若不想使用分页设为0即可   
  16.         String tableName="Place" ; //查询Place表   
  17.         pager.setPageName(tableName);  
  18.         String conditionString="Place_id>0" ; //设置查询条件即sql中where后面的语句   
  19.         pager.setConString(conditionString);  
  20.         int  pageNum=1; //当前显示第1页   
  21.         pager.setCurrentPage(pageNum);  
  22.         Result result = pagerProduct.listProduct(pager);  
  23.         //获取新的page,此时的page已经包所有的信息   
  24.         pager = result.getPager();  
  25.         request.setAttribute("PlaceView" , result);  
  26.         return  mapping.findForward( "PlaceView" );  
  27.     }  
  28. }  

在jsp页面中

  1. <%@ page language= "java"  pageEncoding= "gb2312" %>  
  2.   
  3. <%@ taglib uri="http://struts.apache.org/tags-bean"  prefix= "bean"  %>  
  4. <%@ taglib uri="http://struts.apache.org/tags-logic"  prefix= "logic"  %>  
  5.   
  6. <bean:define id="list"  name= "PlaceView"  property= "content"  type= "java.util.List" ></bean:define>  
  7. <bean:define id="pager"  name= "PlaceView"  property= "pager"  type= "engine.entity.Pager" ></bean:define>  
  8. <bean:define id="hasNextPage"  name= "pager"  property= "hasNextPage"  type= "java.lang.Boolean" ></bean:define>  
  9. <bean:define id="hasPrePage"  name= "pager"  property= "hasPrePage"  type= "java.lang.Boolean" ></bean:define>       
  10.          
  11.         <logic:empty name="list" >  
  12.             <div id="empty" >    
  13.                 <table border="0" >  
  14.                 <tr>  
  15.                     <td colspan="6" >没有可以显示的数据</td>  
  16.                 </tr>  
  17.                 </table>  
  18.             </div>  
  19.         </logic:empty>  
  20.         <logic:notEmpty name="list" >  
  21.             <logic:iterate id="i"  name= "list"  >   
  22.             <div id="place_${i.placeId }" >    
  23.                 <table border="0" >  
  24.                 <tr>  
  25.                     <td>  
  26.                         <input type="checkbox"  name= "selectFlag"  value= "${i.placeId }" >  
  27.                     </td>  
  28.                     <td>${i.placeId }</td>    
  29.                     <td>${i.placeName }</td>  
  30.                     <td>${i.placeNotes }</td>  
  31.                 </tr>    
  32.                 </table>   
  33.               </div>  
  34.             </logic:iterate>    
  35.         </logic:notEmpty>  
  36.       <logic:equal value="false"  name= "hasPrePage" >  
  37.             <span>[上一页]</span>  
  38.       </logic:equal>  
  39.       <logic:equal value="true"  name= "hasPrePage"  >  
  40.             <span><a href="javascript:changePage(${pager.currentPage-1 })"  mce_href= "javascript:changePage(${pager.currentPage-1 })" >[上一页]</a></span>  
  41.       </logic:equal>  
  42.       ${pager.currentPage }/${pager.totalPage }  
  43.       <logic:equal value="false"  name= "hasNextPage" >  
  44.             <span>[下一页]</span>  
  45.       </logic:equal>  
  46.             <logic:equal value="true"  name= "hasNextPage"  >  
  47.             <span><a href="javascript:changePage(${pager.currentPage+1 })"  mce_href= "javascript:changePage(${pager.currentPage+1 })" >[下一页]</a></span>  
  48.       </logic:equal>  

这样所有的分页功能就完成了,至于changePage()这个JavaScript函数就是处理翻页的动作啦,大家应该都知道了吧,就不多说了。

转载From:http://blog.csdn.net/hytfly/article/details/5787462

猜你喜欢

转载自douglaslau.iteye.com/blog/1330649