ssh分页实例

 

ssh分页实例


先是一个page的bean:

Java代码 复制代码
  1. package com.leatherstore.other;    
  2.   
  3. public class Page {    
  4.   
  5. /** 是否有上一页 */    
  6. private boolean hasPrePage;    
  7.   
  8. /** 是否有下一页 */    
  9. private boolean hasNextPage;    
  10.   
  11. /** 每页的数量 */    
  12. private int everyPage;    
  13.   
  14. /** 总页数 */    
  15. private int totalPage;    
  16.   
  17. /** 当前页*/    
  18. private int currentPage;    
  19.   
  20. /** 起始点 */    
  21. private int beginIndex;    
  22.   
  23. /** 总记录数*/    
  24. private int totalCount;    
  25.   
  26. /**   
  27. * @return totalCount   
  28. */    
  29. public int getTotalCount() {    
  30. return totalCount;    
  31. }    
  32.   
  33. /**   
  34. * @param totalCount 要设置的 totalCount   
  35. */    
  36. public void setTotalCount(int totalCount) {    
  37. this.totalCount = totalCount;    
  38. }    
  39.   
  40. /** The default constructor */    
  41. public Page(){    
  42.   
  43. }    
  44.   
  45. /** construct the page by everyPage   
  46. * @param everyPage   
  47. * */    
  48. public Page(int everyPage){    
  49. this.everyPage = everyPage;    
  50. }    
  51.   
  52. /** The whole constructor */    
  53. public Page(boolean hasPrePage, boolean hasNextPage,    
  54. int everyPage, int totalPage,    
  55. int currentPage, int beginIndex,int totalCount) {    
  56. this.hasPrePage = hasPrePage;    
  57. this.hasNextPage = hasNextPage;    
  58. this.everyPage = everyPage;    
  59. this.totalPage = totalPage;    
  60. this.currentPage = currentPage;    
  61. this.beginIndex = beginIndex;    
  62. this.totalCount = totalCount;    
  63. }    
  64.   
  65. /**   
  66. * @return   
  67. * Returns the beginIndex.   
  68. */    
  69. public int getBeginIndex() {    
  70. return beginIndex;    
  71. }    
  72.   
  73. /**   
  74. * @param beginIndex   
  75. * The beginIndex to set.   
  76. */    
  77. public void setBeginIndex(int beginIndex) {    
  78. this.beginIndex = beginIndex;    
  79. }    
  80.   
  81. /**   
  82. * @return   
  83. * Returns the currentPage.   
  84. */    
  85. public int getCurrentPage() {    
  86. return currentPage;    
  87. }    
  88.   
  89. /**   
  90. * @param currentPage   
  91. * The currentPage to set.   
  92. */    
  93. public void setCurrentPage(int currentPage) {    
  94. this.currentPage = currentPage;    
  95. }    
  96.   
  97. /**   
  98. * @return   
  99. * Returns the everyPage.   
  100. */    
  101. public int getEveryPage() {    
  102. return everyPage;    
  103. }    
  104.   
  105. /**   
  106. * @param everyPage   
  107. * The everyPage to set.   
  108. */    
  109. public void setEveryPage(int everyPage) {    
  110. this.everyPage = everyPage;    
  111. }    
  112.   
  113. /**   
  114. * @return   
  115. * Returns the hasNextPage.   
  116. */    
  117. public boolean getHasNextPage() {    
  118. return hasNextPage;    
  119. }    
  120.   
  121. /**   
  122. * @param hasNextPage   
  123. * The hasNextPage to set.   
  124. */    
  125. public void setHasNextPage(boolean hasNextPage) {    
  126. this.hasNextPage = hasNextPage;    
  127. }    
  128.   
  129. /**   
  130. * @return   
  131. * Returns the hasPrePage.   
  132. */    
  133. public boolean getHasPrePage() {    
  134. return hasPrePage;    
  135. }    
  136.   
  137. /**   
  138. * @param hasPrePage   
  139. * The hasPrePage to set.   
  140. */    
  141. public void setHasPrePage(boolean hasPrePage) {    
  142. this.hasPrePage = hasPrePage;    
  143. }    
  144.   
  145. /**   
  146. * @return Returns the totalPage.   
  147.  
  148. */    
  149. public int getTotalPage() {    
  150. return totalPage;    
  151. }    
  152.   
  153. /**   
  154. * @param totalPage   
  155. * The totalPage to set.   
  156. */    
  157. public void setTotalPage(int totalPage) {    
  158. this.totalPage = totalPage;    
  159. }    
  160. }   
然后构建一个page的工厂PageUtil:
Java代码 复制代码
  1. package com.leatherstore.other;    
  2.   
  3. public class PageUtil {    
  4. /**   
  5. * Use the origin page to create a new page   
  6.  
  7. * @param page   
  8. * @param totalRecords   
  9. * @return   
  10. */    
  11. public static Page createPage(Page page, int totalRecords) {    
  12. return createPage(page.getEveryPage(), page.getCurrentPage(),    
  13. totalRecords);    
  14. }    
  15.   
  16. /**   
  17. * the basic page utils not including exception handler   
  18.  
  19. * @param everyPage   
  20. * @param currentPage   
  21. * @param totalRecords   
  22. * @return page   
  23. */    
  24. public static Page createPage(int everyPage, int currentPage,    
  25. int totalRecords) {    
  26. everyPage = getEveryPage(everyPage);    
  27. currentPage = getCurrentPage(currentPage);    
  28. int beginIndex = getBeginIndex(everyPage, currentPage);    
  29. int totalPage = getTotalPage(everyPage, totalRecords);    
  30. boolean hasNextPage = hasNextPage(currentPage, totalPage);    
  31. boolean hasPrePage = hasPrePage(currentPage);    
  32.   
  33. return new Page(hasPrePage, hasNextPage, everyPage, totalPage,    
  34. currentPage, beginIndex, totalRecords);    
  35. }    
  36.   
  37. private static int getEveryPage(int everyPage) {    
  38. return everyPage == 0 ? 10 : everyPage;    
  39. }    
  40.   
  41. private static int getCurrentPage(int currentPage) {    
  42. return currentPage == 0 ? 1 : currentPage;    
  43. }    
  44.   
  45. private static int getBeginIndex(int everyPage, int currentPage) {    
  46. return (currentPage - 1) * everyPage;    
  47. }    
  48.   
  49. private static int getTotalPage(int everyPage, int totalRecords) {    
  50. int totalPage = 0;    
  51.   
  52. if (totalRecords % everyPage == 0)    
  53. totalPage = totalRecords / everyPage;    
  54. else    
  55. totalPage = totalRecords / everyPage + 1;    
  56.   
  57. return totalPage;    
  58. }    
  59.   
  60. private static boolean hasPrePage(int currentPage) {    
  61. return currentPage == 1 ? false : true;    
  62. }    
  63.   
  64. private static boolean hasNextPage(int currentPage, int totalPage) {    
  65. return currentPage == totalPage || totalPage == 0 ? false : true;    
  66. }    
  67.   
  68. }   
然后建一个专用的bean:
Java代码 复制代码
  1. package com.leatherstore.hibernate.domain;    
  2.   
  3. import java.util.List;    
  4.   
  5. import com.leatherstore.other.Page;    
  6.   
  7. public class Result {    
  8. private Page page; //分页信息    
  9. private List content; //每页显示的集合    
  10. /**   
  11. * The default constructor   
  12. */    
  13. public Result() {    
  14. super();    
  15. }    
  16.   
  17. /**   
  18. * The constructor using fields   
  19.  
  20. * @param page   
  21. * @param content   
  22. */    
  23. public Result(Page page, List content) {    
  24. this.page = page;    
  25. this.content = content;    
  26. }    
  27.   
  28. /**   
  29. * @return Returns the content.   
  30. */    
  31. public List getContent() {    
  32. return content;    
  33. }    
  34.   
  35. /**   
  36. * @return Returns the page.   
  37. */    
  38. public Page getPage() {    
  39. return page;    
  40. }    
  41.   
  42. /**   
  43. * @param content   
  44. * The content to set.   
  45. */    
  46. public void setContent(List content) {    
  47. this.content = content;    
  48. }    
  49.   
  50. /**   
  51. * @param page   
  52. * The page to set.   
  53. */    
  54. public void setPage(Page page) {    
  55. this.page = page;    
  56. }    
  57. }   


然后在数据访问层写两个方法:
Java代码 复制代码
  1. ProductDAO:    
  2. public List getProductByPage(Page page);    
  3. public int getProductCount(); //返回数据的总数    
  4. ProductDAO的接口实现ProductDAOImpl:    
  5. //为了在spring里统一编程风格:我用的回调的方法    
  6. public List getProductByPage(final Page page) {    
  7. return this.getHibernateTemplate().executeFind(new HibernateCallback(){    
  8. public Object doInHibernate(Session session) throws HibernateException, SQLException {    
  9. Query query=session.createQuery("from Productinfo");    
  10. query.setFirstResult(page.getBeginIndex()); //hibernate分页的精髓 呵呵    
  11. query.setMaxResults(page.getEveryPage());    
  12. return query.list();    
  13. }    
  14. });    
  15. }    
  16.   
  17. public int getProductCount() {    
  18. List list=this.getHibernateTemplate().find("select count(*) from Productinfo");    
  19. return ((Integer)list.iterator().next()).intValue();    
  20. }   
然后是业务层:
Java代码 复制代码
  1. IProduct:    
  2. public Result listProduct(Page page);   
然后IProduct接口的实现:IProductImpl:
Java代码 复制代码
  1. private ProductDAO productDAO;    
  2. public void setProductDAO(ProductDAO productDAO){    
  3. this.productDAO=productDAO;    
  4. }    
  5.   
  6. public Result listProduct(Page page) {    
  7. int totalRecords = this.productDAO.getProductCount();    
  8. page = PageUtil.createPage(page, totalRecords);    
  9. List products = this.productDAO.getProductByPage(page);    
  10. return new Result(page, products);    
  11. }   
然后再代理层:
Java代码 复制代码
  1. ProductProxy:    
  2. IProduct pro=(IProduct)AppContext.getInstance().getappcontext().getBean("productServicewithTran");    
  3.   
  4. public Result productlist(Page page){    
  5. try{    
  6. return pro.listProduct(page);    
  7. }catch(DataAccessException ex){    
  8. ex.printStackTrace();    
  9. return null;    
  10. }    
  11. }   
呵呵 终于到productAction啦
显示前方法的代码
Java代码 复制代码
  1. Page page = new Page(); //实例化一个page对象    
  2. page.setEveryPage(10); //设置每页显示的条数    
  3. page.setCurrentPage(1); //为第一页    
  4. Result result = pdp.productlist(page);    
  5. request.setAttribute("page", pageinfo);    
  6. request.setAttribute("productlist", list);    
  7. return mapping.findForward("showProduct");   
接着就是jsp页面了
Html代码 复制代码
  1. <logic:iterate id="product" name="productlist">    
  2. //中间迭代所要显示的数据    
  3. </logic:iterate>    
  4. <tr>    
  5. <td width="80" height="30"> </td>    
  6. <logic:equal value="true" name="page" property="hasPrePage">    
  7. <td width="150" height="30"><div align="right"><a href="../product.do?method=showProductByTag&index=first&msg=${msg }">首页</a></div></td>    
  8. <td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=prew&pageno=${page.currentPage -1}&msg=${msg }">上一页</a></div></td>    
  9. </logic:equal>    
  10. <logic:notEqual value="true" name="page" property="hasPrePage">    
  11. <td width="150" height="30"><div align="right">首页</div></td>    
  12. <td width="80" height="30"><div align="center">上一页</div></td>    
  13. </logic:notEqual>    
  14. <logic:equal value="true" name="page" property="hasNextPage">    
  15. <td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=next&pageno=${page.currentPage +1 }&msg=${msg }">下一页</a></div></td>    
  16. <td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=end&pageno=${page.totalPage }&msg=${msg }">尾页</a></div></td>    
  17. </logic:equal>    
  18. <logic:notEqual value="true" name="page" property="hasNextPage">    
  19. <td width="80" height="30"><div align="center">下一页</div></td>    
  20. <td width="80" height="30"><div align="center">尾页</div></td>    
  21. </logic:notEqual>    
  22. <td height="30" colspan="3"><div align="center">页次${page.currentPage }/${page.totalPage }   共${page.totalCount }条记录</div> <div align="center"></div></td>    
  23. </tr>   
可以显示相应的页面信息
然后productAction里面的showProductByTag代码如下:
Java代码 复制代码
  1. Page page = new Page();    
  2. page.setEveryPage(10);    
  3. String pagemark = request.getParameter("goto");    
  4. if (pagemark == null) {    
  5. String state = request.getParameter("index");    
  6. String pageno = request.getParameter("pageno");    
  7. System.out.println("pageno=" + pageno);    
  8. if ("first".equals(state)) {    
  9. page.setCurrentPage(1);    
  10. Result result = pdp.productlist(page);    
  11. request.setAttribute("page", result.getPage());    
  12. request.setAttribute("productlist", result.getContent());    
  13. else if ("prew".equals(state)) {    
  14. page.setCurrentPage(Integer.parseInt(pageno));    
  15. Result result = pdp.productlist(page);    
  16. request.setAttribute("page", result.getPage());    
  17. request.setAttribute("productlist", result.getContent());    
  18. else if ("next".equals(state)) {    
  19. page.setCurrentPage(Integer.parseInt(pageno));    
  20. Result result = pdp.productlist(page);    
  21. request.setAttribute("page", result.getPage());    
  22. request.setAttribute("productlist", result.getContent());    
  23. else if ("end".equals(state)) {    
  24. page.setCurrentPage(Integer.parseInt(pageno));    
  25. Result result = pdp.productlist(page);    
  26. request.setAttribute("page", result.getPage());    
  27. request.setAttribute("productlist", result.getContent());    
  28. }    
  29. else {    
  30. page.setCurrentPage(Integer.parseInt(pagemark));    
  31. Result result = pdp.productlist(page);    
  32. request.setAttribute("page", result.getPage());    
  33. request.setAttribute("productlist", result.getContent());    
  34. }    
  35. return mapping.findForward("showProduct");   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值