spring+hibernate+pager taglib實現分頁(底層也做了分頁)(二)

現在說中間的控制層。

在Struts的FORM中,增加private int pageDisplay = 10; 屬性,並增加對應的setter和getter方法。這個屬性是用來設置每頁顯示的記錄數,10是默認值,可以根據實際情況進行設置。

在Struts的ACTION層,增加一個方法,代碼如下
java 代碼
  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)。
發佈了0 篇原創文章 · 獲贊 0 · 訪問量 1162
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章