分页封装实用工具类及其使用方法
分页封装实用工具类及其使用方法
作者: javaboy2012
Email:yanek@163.com
qq: 1046011462
package com.yanek.util; import java.util.List; public class PageBean { /** * @param args */ public static void main(String[] args) { } private int currentpage; // 当前页数 private int pagesize; // 每页大小 private int totalPagecount; // 总页数 private int totalRecordCount; // 总记录数 private List recordContents; // 当前页记录内容列表 public int getCurrentpage() { return currentpage; } public int getPagesize() { return pagesize; } public List getRecordContents() { return recordContents; } public int getTotalPagecount() { return totalPagecount; } public int getTotalRecordCount() { return totalRecordCount; } public PageBean(int currentpage, int pagesize, int totalRecordCount, List recordContents) { super(); this.currentpage = currentpage; this.pagesize = pagesize; this.totalRecordCount = totalRecordCount; this.recordContents = recordContents; if (totalRecordCount % pagesize == 0) { this.totalPagecount = totalRecordCount / pagesize; } else { this.totalPagecount = totalRecordCount / pagesize + 1; } } } 调用方法: action里调用: public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { TopicDAO tdao=new TopicDAOImpl(); String s_pagesize=(String)request.getParameter("pagesize"); int pagesize=20; if (s_pagesize!=null) { pagesize=Integer.parseInt(s_pagesize); } int pagecount=1; String s_pagecount=(String)request.getParameter("pagecount"); if (s_pagecount!=null) { pagecount=Integer.parseInt(s_pagecount); } //得到记录总数 long totalcount=tdao.getTopicTotalCount(); System.out.println("total:"+totalcount); List topics=tdao.getTopics(pagesize,pagecount); for (int i=0;i<topics.size();i++) { TopicBean tb=(TopicBean)topics.get(i); System.out.println(tb.getTid()+"-"+tb.getTname()); } PageBean pb=new PageBean(pagecount,pagesize, (int)totalcount,topics); request.setAttribute("topic_pagebean", pb); return mapping.findForward("topic_list"); } jsp页面调用示例: <% PageBean pb=(PageBean)request.getAttribute("topic_pagebean"); List topics=pb.getRecordContents(); %> <div class="cont"> <center><h2>记录列表</h2></center><br/><br/> <div class="list"> <form name="topic" action="topic.do" method="get"> <input type="hidden" name="method" value="list" /> <input type="hidden" name="pagesize" value="<%=pb.getPagesize()%>" /> <table width="100%" border="0"> <tr class="white"> <th class="sl1" width="30%">标题</th> <th width="30%" class="sl1">描述</th> <th class="sl1" width="25%">ID </th> <th width="15%"> 操作</th> </tr> <% for (int i=0;i<topics.size();i++) { TopicBean tb=(TopicBean)topics.get(i); %> <% String classname=""; if (i%2==1) { classname="white"; } %> <tr class="<%=classname %>"> <td><%=tb.getTname() %></td> <td><%=tb.getTdesc() %></td> <td> <%=tb.getTid() %> </td> <td><a href='/topic.do?method=view&tid=<%=tb.getTid() %>'>[查看]</a></td> </tr> <% } %> <tr> <td colspan="6"> <p align="center"> <br/> 共<%=pb.getTotalRecordCount() %>个记录,共<%=pb.getTotalPagecount() %>页,每页<%=pb.getPagesize() %>个 ,当前第<%=pb.getCurrentpage() %>页 <% if (pb.getCurrentpage()==1) { out.print("首页 上页 "); } else { %> <a href="topic.do?method=list&pagecount=1&pagesize=<%=pb.getPagesize() %>">首页</a> <a href="topic.do?method=list&pagecount=<%=pb.getCurrentpage()-1%>&pagesize=<%=pb.getPagesize() %>">上页</a> <% } %> <% if (pb.getCurrentpage()==pb.getTotalPagecount()) { out.print("下一页 尾页 "); } else { %> <a href="topic.do?method=list&pagecount=<%=pb.getCurrentpage()+1 %>&pagesize=<%=pb.getPagesize() %>">下一页 </a> <a href="topic.do?method=list&pagecount=<%=pb.getTotalPagecount() %>&pagesize=<%=pb.getPagesize() %>"> 尾页</a> <% } %> 转到第 <input type="text" name="pagecount" value="<%=pb.getCurrentpage() %>" size="4"> 页 <input type="submit" value="GO" size="4"> </td> </tr> </table></form> </div> </div> action配置 <action path="/topic" type="com.myweb.web.action.TopicAction" scope="request" parameter="method"> <forward name="topic_list" path="/WEB-INF/pages/test/test/topic_list.jsp"/> </action> 访问方式: http://192.168.0.1/topic.do?method=list&pagecount=1&pagesize=10