读书人

spring+hibernate+pager taglib兑现分

发布时间: 2012-11-10 10:48:50 作者: rapoo

spring+hibernate+pager taglib实现分页(底层也做了分页)(二)
现在说中间的控制层。

在Struts的FORM中,增加private int pageDisplay = 10; 属性,并增加对应的setter和getter方法。这个属性是用来设置每页显示的记录数,10是默认值,可以根据实际情况进行设置。

在Struts的ACTION层,增加一个方法,代码如下

  1. package?com.excellence.struts.action; ??
  2. ??
  3. ??
  4. import?java.util.List; ??
  5. ??
  6. ??
  7. import?javax.servlet.http.HttpServletRequest; ??
  8. ??
  9. import?javax.servlet.http.HttpServletResponse; ??
  10. ??
  11. ??
  12. import?org.apache.struts.action.Action; ??
  13. ??
  14. import?org.apache.struts.action.ActionForm; ??
  15. ??
  16. import?org.apache.struts.action.ActionForward; ??
  17. ??
  18. import?org.apache.struts.action.ActionMapping; ??
  19. ??
  20. ??
  21. import?com.excellence.page.Service; ??
  22. ??
  23. import?com.excellence.struts.form.CheckForm; ??
  24. ??
  25. public?class?CheckAction?extends?Action?{ ??
  26. ??
  27. public?ActionForward?execute( ??
  28. ??
  29. ActionMapping?mapping, ??
  30. ??
  31. ActionForm?form, ??
  32. ??
  33. HttpServletRequest?request, ??
  34. ??
  35. HttpServletResponse?response)?{ ??
  36. ??
  37. ??
  38. ??
  39. setSubPage(request,form); ??
  40. ??
  41. ??
  42. ??
  43. return?mapping.findForward("result"); ??
  44. ??
  45. } ??
  46. ??
  47. ??
  48. ??
  49. private?void?setSubPage(HttpServletRequest?request,ActionForm?form){ ??
  50. ??
  51. CheckForm?thisForm?=?(CheckForm)form; ??
  52. ??
  53. String?content?=?thisForm.getContent();//查询条件中的内容 ??
  54. ??
  55. Service?service?=?new?Service(); ??
  56. ??
  57. ??
  58. ??
  59. //拿到每页要显示的记录数 ??
  60. ??
  61. int?pageSize?=?thisForm.getPageDisplay(); ??
  62. ??
  63. request.setAttribute("pageSize",pageSize+""); ??
  64. ??
  65. //拿到目前要显示得页数 ??
  66. ??
  67. int?pageNumber?=?1; ??
  68. ??
  69. String?strPageNumber?=?request.getParameter("page"); ??
  70. ??
  71. if(strPageNumber?!=?null) ??
  72. ??
  73. pageNumber?=?Integer.parseInt(strPageNumber); ??
  74. ??
  75. //计算要显示的页数得第一条记录的位置 ??
  76. ??
  77. int?start?=?0; ??
  78. ??
  79. start?=?(pageNumber?-?1)*pageSize; ??
  80. ??
  81. ??
  82. ??
  83. List?result?=?service.findByCondition("from?Subpage?where?content?like???order?by?content",new?Object[]{content},start,pageSize);? ??
  84. ??
  85.   ?List?counts?=?service.findByCondition("select?count(*)?from?Subpage?where?content?like??",new?Object[]{content}); ??
  86. ??
  87. int?count?=?Integer.parseInt(counts.get(0).toString()); ??
  88. ??
  89. request.setAttribute("count",count+"");//总条数 ??
  90. ??
  91. ??
  92. ??
  93. //?设置总页数 ??
  94. ??
  95. int?totalPage?=?count?%?pageSize?==?0???count?/?pageSize?:?(count?/?pageSize?+?1); ??
  96. ??
  97. request.setAttribute("totalPage",totalPage+""); ??
  98. ??
  99. ??
  100. ??
  101. request.setAttribute("result",result); ??
  102. ??
  103. if(result.size()?==?0) ??
  104. ??
  105. request.setAttribute("result",null); ??
  106. ??
  107. } ??
  108. ??
  109. } ??
  110. ??
  111. ??
主要就是增加了一个私有的方法private void setSubPage(HttpServletRequest request,ActionForm form)。
1 楼 wubaodan 2007-02-04 你的JSP放上来看看 2 楼 superallen 2007-02-04 JSP的内容已经写出来了

读书人网 >软件架构设计

热点推荐