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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值