分页工具类

 
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  ,20
	
	Integer 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' οnchange=\"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 οnchange=\"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();
	}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值