spring+hibernate+pager taglib实现分页(底层也做了分页)(二)
现在说中间的控制层。
在Struts的FORM中,增加private int pageDisplay = 10; 属性,并增加对应的setter和getter方法。这个属性是用来设置每页显示的记录数,10是默认值,可以根据实际情况进行设置。
在Struts的ACTION层,增加一个方法,代码如下
- package?com.excellence.struts.action; ??
- ??
- ??
- import?java.util.List; ??
- ??
- ??
- import?javax.servlet.http.HttpServletRequest; ??
- ??
- import?javax.servlet.http.HttpServletResponse; ??
- ??
- ??
- import?org.apache.struts.action.Action; ??
- ??
- import?org.apache.struts.action.ActionForm; ??
- ??
- import?org.apache.struts.action.ActionForward; ??
- ??
- import?org.apache.struts.action.ActionMapping; ??
- ??
- ??
- import?com.excellence.page.Service; ??
- ??
- import?com.excellence.struts.form.CheckForm; ??
- ??
- public?class?CheckAction?extends?Action?{ ??
- ??
- public?ActionForward?execute( ??
- ??
- ActionMapping?mapping, ??
- ??
- ActionForm?form, ??
- ??
- HttpServletRequest?request, ??
- ??
- HttpServletResponse?response)?{ ??
- ??
- ??
- ??
- setSubPage(request,form); ??
- ??
- ??
- ??
- return?mapping.findForward("result"); ??
- ??
- } ??
- ??
- ??
- ??
- private?void?setSubPage(HttpServletRequest?request,ActionForm?form){ ??
- ??
- CheckForm?thisForm?=?(CheckForm)form; ??
- ??
- String?content?=?thisForm.getContent();//查询条件中的内容 ??
- ??
- Service?service?=?new?Service(); ??
- ??
- ??
- ??
- //拿到每页要显示的记录数 ??
- ??
- int?pageSize?=?thisForm.getPageDisplay(); ??
- ??
- request.setAttribute("pageSize",pageSize+""); ??
- ??
- //拿到目前要显示得页数 ??
- ??
- int?pageNumber?=?1; ??
- ??
- String?strPageNumber?=?request.getParameter("page"); ??
- ??
- if(strPageNumber?!=?null) ??
- ??
- pageNumber?=?Integer.parseInt(strPageNumber); ??
- ??
- //计算要显示的页数得第一条记录的位置 ??
- ??
- int?start?=?0; ??
- ??
- start?=?(pageNumber?-?1)*pageSize; ??
- ??
- ??
- ??
- List?result?=?service.findByCondition("from?Subpage?where?content?like???order?by?content",new?Object[]{content},start,pageSize);? ??
- ??
- ?List?counts?=?service.findByCondition("select?count(*)?from?Subpage?where?content?like??",new?Object[]{content}); ??
- ??
- int?count?=?Integer.parseInt(counts.get(0).toString()); ??
- ??
- request.setAttribute("count",count+"");//总条数 ??
- ??
- ??
- ??
- //?设置总页数 ??
- ??
- int?totalPage?=?count?%?pageSize?==?0???count?/?pageSize?:?(count?/?pageSize?+?1); ??
- ??
- request.setAttribute("totalPage",totalPage+""); ??
- ??
- ??
- ??
- request.setAttribute("result",result); ??
- ??
- if(result.size()?==?0) ??
- ??
- request.setAttribute("result",null); ??
- ??
- } ??
- ??
- } ??
- ??
- ??
1 楼 wubaodan 2007-02-04 你的JSP放上来看看 2 楼 superallen 2007-02-04 JSP的内容已经写出来了