分页工具类

来源:互联网 发布:唐代男子服饰知乎 编辑:程序博客网 时间:2024/04/28 11:40
 
package com.xmc.cms.utils;import java.util.Collection;import java.util.Iterator;import java.util.Map;import javax.servlet.http.HttpServletRequest;public class SplitPageBean {HttpServletRequest request;public SplitPageBean() {}// 总数量int count ;int pages ; // 共有多少页Integer pageNum =this.curpage; // 每页多少条默认5条int pagesUP=3 ; //页面增长率,如每页 5 条 后三 10,15  ,20Integer curpage=6 ; // 当前的页数int begin ;// 开始的页数int end;//参数String param="";String pageString;public SplitPageBean(HttpServletRequest request, Collection data,int sumRow) {this.request=request;// 总数量count = sumRow;request.setAttribute("items", data);// 每页多少条if(request.getSession().getAttribute("pageNum")!=null){pageNum=(Integer)request.getSession().getAttribute("pageNum");}String row = request.getParameter("pageNum");if (row != null) {pageNum = Integer.parseInt(row);// 每页多少条}else{pageNum=6;}if (count % pageNum == 0) {pages = count / pageNum;} else {pages = count / pageNum + 1;}// 当前的页数try {curpage = Integer.parseInt(request.getParameter("curpage"));} catch (Exception e) {curpage=(Integer)request.getSession().getAttribute("curpage");if(curpage==null)curpage = 1;}if(curpage>pages){curpage=1;}begin = (curpage - 1) * pageNum;// 开始的页数end = begin + pageNum - 1;request.setAttribute("pages", pages);request.setAttribute("pageNum", pageNum);request.setAttribute("curpage", curpage);request.setAttribute("begin", begin);request.setAttribute("end", end);Map map=request.getParameterMap();StringBuffer sb=new StringBuffer("");for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {Object object = (Object) iterator.next();String sname=object.toString(); if(sname.equals("curpage")||sname.equals("pageNum"))continue;String svalue=request.getParameter(sname);sb.append("&"+sname+"="+svalue);}param=sb.toString();pageString= this.getPageStringStyle();request.setAttribute("pageString",pageString);}public String getPageStringStyle() {StringBuffer bufstr = new StringBuffer();bufstr.append("每页" + this.pageNum + "条记录   ");bufstr.append("共" + this.count + "条记录    ");bufstr.append("第" + this.curpage + "页    ");bufstr.append("共" + this.pages + "页    ");//拼接连接语句bufstr.append("<a "+(this.curpage<=1?"disabled":"href=\"javaScript:window.location='paginationManage.do?curpage="+(1)+"&pageNum="+pageNum+param+"'\"")+">首页</a>  "+"<a  "+(this.curpage<=1?"disabled":"href=\"javaScript:window.location='paginationManage.do?curpage="+(this.curpage-1)+"&pageNum="+pageNum+param+"'\"")+">上一页</a>  "+"<a  "+(this.curpage>=pages?"disabled":"href=\"javaScript:window.location='paginationManage.do?curpage="+(this.curpage+1)+"&pageNum="+pageNum+param+"'\"")+">下一页</a>  "+"<a "+(this.curpage>=pages?"disabled":"href=\"javaScript:window.location='paginationManage.do?curpage="+(pages)+"&pageNum="+pageNum+param+"'\"")+">尾页  </a>"); bufstr.append("    转到:");bufstr.append("<select id='_gotoPageCount' onchange=\"window.location='paginationManage.do"+"?curpage='+this.value+'&pageNum="+this.pageNum+param+"'\""+">");String select = "";for (int i = 1; i <= this.pages; i++) {if (i == this.curpage) {select = " selected ";} else {select = "";}bufstr.append("<option " + select + " value=" + i + " >第" + i+ "页</option>\r\n");}bufstr.append("</select>");bufstr.append("    ");bufstr.append("<select onchange=\"window.location='paginationManage.do"+"?pageNum='+this.value+'"+param+"'\""+//   +'&curpage="+this.curpage+"'\""+">\r\n");select = "";//5个选择for (int i = 1; i <= 5; i++) {if (this.pageNum==(i*pagesUP)) {select = " selected ";} else {select = "";}bufstr.append("<option " + select + " value="+ pagesUP * i + " >每页"+ (pagesUP * i) + "条记录</option>\r\n");}bufstr.append("</select>");request.getSession().setAttribute("curpage", this.curpage);request.getSession().setAttribute("pageNum", this.pageNum);return bufstr.toString();}}