读书人

一个十分强大的SSH分页方法

发布时间: 2012-10-27 10:42:26 作者: rapoo

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

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

?

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

?

首先是分页实体类:

?

view plain
  1. package?engine.entity;????
  2. import?java.util.List;????
  3. public?class?Pager?{??????/**?
  4. ?????*??是否有上一页???????*/??
  5. ????private?boolean?hasPrePage;??????/**?
  6. ?????*?是否有下一页???????*/??
  7. ????private?boolean?hasNextPage;??????/**?
  8. ?????*??每页的数量???????*/??
  9. ????private?int?everyPage;??????/**?
  10. ?????*??总页数??????*/??
  11. ????private?int?totalPage;??????/**?
  12. ?????*?当前页???????*/??
  13. ????private?int?currentPage;???????/**?
  14. ?????*??起始点???????*/??
  15. ????private?int?beginIndex;???????/**?
  16. ?????*??总记录数??????*/??
  17. ????private?int?totalCount;???????/**?
  18. ?????*?该页名称??????*/??
  19. ????private?String?pageName;???????/**?
  20. ?????*?查询条件??????*/??
  21. ????private?String?conString;??????public?String?getConString()?{??
  22. ????????return?conString;??????}??
  23. ????/**??????*?设置查询条件?
  24. ?????*/??????public?void?setConString(String?conditionString)?{??
  25. ????????this.conString?=?conditionString;??????}??
  26. ????/**??????*?设置该页名称(即查询表名)?
  27. ?????*/??????public?String?getPageName()?{??
  28. ????????return?pageName;??????}??
  29. ??????public?void?setPageName(String?tableName)?{??
  30. ????????this.pageName?=?tableName;??????}??
  31. ??????public?int?getTotalCount()?{??
  32. ????????return?totalCount;??????}??
  33. ????/**??????*??设置总记录数?
  34. ?????*/??????public?void?setTotalCount(int?totalCount)?{??
  35. ????????this.totalCount?=?totalCount;??????}??
  36. ????//?construct?the?page?by?everyPage???????public?Pager(int?everyPage){??
  37. ????????this.everyPage?=?everyPage;??????}??
  38. ????//The?whole?constructor???????public?Pager(boolean?hasPrePage,?boolean?hasNextPage,???
  39. ????????????????????int?everyPage,?int?totalPage,??????????????????????int?currentPage,?int?beginIndex,int?totalCount,??
  40. ????????????????????String?pageName,String?conString)?{??????????this.hasPrePage?=?hasPrePage;??
  41. ????????this.hasNextPage?=?hasNextPage;??????????this.everyPage?=?everyPage;??
  42. ????????this.totalPage?=?totalPage;??????????this.currentPage?=?currentPage;??
  43. ????????this.beginIndex?=?beginIndex;??????????this.totalCount?=?totalCount;??
  44. ????????this.pageName?=?pageName;??????????this.conString?=?conString;??
  45. ????}??????public?int?getBeginIndex()?{??
  46. ????????return?beginIndex;??????}??
  47. ????/**??????*??设置起始点??
  48. ?????*/??????public?void?setBeginIndex(int?beginIndex)?{??
  49. ????????this.beginIndex?=?beginIndex;??????}??
  50. ????public?int?getCurrentPage()?{??????????return?currentPage;??
  51. ????}??????/**?
  52. ?????*?设置当前页???????*/??
  53. ????public?void?setCurrentPage(int?currentPage)?{??????????this.currentPage?=?currentPage;??
  54. ????}??????public?int?getEveryPage()?{??
  55. ????????return?everyPage;??????}??
  56. ????/**??????*??设置每页的数量??
  57. ?????*/??????public?void?setEveryPage(int?everyPage)?{??
  58. ????????this.everyPage?=?everyPage;??????}??
  59. ????/**??????*?是否有下一页??
  60. ?????*/??????public?boolean?getHasNextPage()?{??
  61. ????????return?hasNextPage;??????}??
  62. ????public?void?setHasNextPage(boolean?hasNextPage)?{??????????this.hasNextPage?=?hasNextPage;??
  63. ????}??????/**?
  64. ?????*?是否有上一页???????*/??
  65. ????public?boolean?getHasPrePage()?{??????????return?hasPrePage;??
  66. ????}??????public?void?setHasPrePage(boolean?hasPrePage)?{??
  67. ????????this.hasPrePage?=?hasPrePage;??????}??
  68. ????public?int?getTotalPage()?{??????????return?totalPage;??
  69. ????}??????/**?
  70. ?????*??设置总页数??????*/??
  71. ????public?void?setTotalPage(int?totalPage)?{??????????this.totalPage?=?totalPage;??
  72. ????}????
  73. }??

?

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

?

view plain
  1. package?engine.entity;????
  2. import?java.util.List;????
  3. public?class?Result?{??????private?Pager?pager;????
  4. ????private?List?content;??????
  5. ?????????//The?default?constructor???????????public?Result()?{??
  6. ????????????super();??????????}???
  7. ?????????/**???????????*?@param?分页信息?
  8. ??????????*?@param?每页显示的集合???????????*/??
  9. ????????public?Result(Pager?pager,?List?content)?{??????????????this.pager?=?pager;??
  10. ????????????this.content?=?content;??????????}??
  11. ???????????
  12. ????????public?List?getContent()?{??????????????return?content;??
  13. ????????}????
  14. ?????????????????public?Pager?getPager()?{??
  15. ????????????return?pager;??????????}??
  16. ???????????
  17. ????????public?void?setContent(List?content)?{??????????????this.content?=?content;??
  18. ????????}????
  19. ?????????????????public?void?setPager(Pager?pager)?{??
  20. ????????????this.pager?=?pager;??????????}??
  21. }??

?

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

?

view plain
  1. package?engine;????
  2. import?engine.entity.Pager;????
  3. public?class?PagerUtil?{??????//?Use?the?origin?page?to?create?a?new?page??
  4. ????
  5. ????public?static?Pager?createPage(Pager?page,?int?totalRecords)?{??????????return?createPage(page.getEveryPage(),?page.getCurrentPage(),page.getPageName(),??
  6. ????????????????page.getConString(),totalRecords);??????}??
  7. ????
  8. ????//the?basic?page?utils?not?including?exception?handler????
  9. ??????public?static?Pager?createPage(int?everyPage,?int?currentPage,??
  10. ????????????String?pageName,String?conString,int?totalRecords)?{??????????everyPage?=?getEveryPage(everyPage);??
  11. ????????currentPage?=?getCurrentPage(currentPage);??????????int?beginIndex?=?getBeginIndex(everyPage,?currentPage);??
  12. ????????int?totalPage?=?getTotalPage(everyPage,?totalRecords);??????????boolean?hasNextPage?=?hasNextPage(currentPage,?totalPage);??
  13. ????????boolean?hasPrePage?=?hasPrePage(currentPage);??????????System.out.println(everyPage+"*"+totalPage+"*"+??
  14. ????????????????currentPage+"*"+beginIndex+"*"+totalRecords);??????????return?new?Pager(hasPrePage,?hasNextPage,?everyPage,?totalPage,??
  15. ????????????????currentPage,?beginIndex,?totalRecords,?pageName,?conString);??????}??
  16. ??????private?static?int?getEveryPage(int?everyPage)?{??
  17. ????????return?everyPage?==?0???10?:?everyPage;??????}??
  18. ??????private?static?int?getCurrentPage(int?currentPage)?{??
  19. ????????return?currentPage?==?0???1?:?currentPage;??????}??
  20. ??????private?static?int?getBeginIndex(int?everyPage,?int?currentPage)?{??
  21. ????????return?(currentPage?-?1)?*?everyPage;??????}??
  22. ??????private?static?int?getTotalPage(int?everyPage,?int?totalRecords)?{??
  23. ????????int?totalPage?=?0;????????
  24. ????????if?(totalRecords?%?everyPage?==?0)??????????????totalPage?=?totalRecords?/?everyPage;??
  25. ????????else??????????????totalPage?=?totalRecords?/?everyPage?+?1;??
  26. ??????????????return?totalPage;??
  27. ????}????
  28. ????private?static?boolean?hasPrePage(int?currentPage)?{??????????return?currentPage?==?1???false?:?true;??
  29. ????}????
  30. ????private?static?boolean?hasNextPage(int?currentPage,?int?totalPage)?{??????????return?currentPage?==?totalPage?||?totalPage?==?0???false?:?true;??
  31. ????}????
  32. }??

?

数据访问层接口:

?

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

?

数据访问层接口实现:

?

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

?

业务层接口:

?

view plain
  1. package?engine;????
  2. import?engine.entity.Pager;??import?engine.entity.Result;??
  3. ??public?interface?PagerProduct?{??
  4. ????public?Result?listProduct(Pager?pager);??}??

?

业务层接口实现:

?

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

?

呼~~终于到productAction啦

?

?

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

在jsp页面中

?

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

?

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

?

?

?

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

读书人网 >软件架构设计

热点推荐