一个好用的分页javaBean_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 一个好用的分页javaBean

一个好用的分页javaBean

 2013/7/30 19:08:48  sky52bc  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.rmb.util;importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;importjavax.servlet.http.HttpServletRequest;/***分页JavaBean***/publicclassPageBean{/*当前页*/privateintcurPage=1;/*总记录数*/privateinttotalRecord=0
  • 标签:Java 一个
class="java">
package com.rmb.util;



import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 分页JavaBean
 * 
 * 
 */
public class PageBean {
	/* 当前页 */
	private int curPage = 1;

	/* 总记录数 */
	private int totalRecord = 0;

	/* 每页记录数 */
	private int pageRecord = 10;

	/* 是否分页,默认分页 */
	private boolean paginate = true;

	/* 请求的URL */
	private String url;

	/* 请求中的参数 */
	private Map<String, String[]> parameters = new HashMap<String, String[]>();

	public PageBean() {
		super();
	}

	public PageBean(HttpServletRequest request) {
		super();
		this.setRequest(request);
	}

	/**
	 * 使用HttpServletRequest对象对分页Bean进行初始化
	 * 
	 */
	@SuppressWarnings("unchecked")
	public void setRequest(HttpServletRequest request) {
		this.setCurPage(request.getParameter("curPage"));// 从请求中获取当前页
		this.setPageRecord(request.getParameter("pageRecord"));// 从请求中获取页大小
		this.setPaginate(request.getParameter("paginate"));// 获取用来判断是否分页的参数
		this.setUrl(request.getContextPath() + request.getServletPath());// 获取表示请求资源的URL
		this.setParameters(new HashMap<String, String[]>(request
				.getParameterMap()));// 获取请求中的所有参数
	}

	/**
	 * true表示进行分页
	 * 
	 * @return
	 */
	public boolean isPaginate() {
		return paginate;
	}

	/**
	 * 设置是否分页true分页,false不分页
	 * 
	 * @param paginate
	 */
	public void setPaginate(boolean paginate) {
		this.paginate = paginate;
	}

	/**
	 * 设置是否分页true分页,false不分页
	 * 
	 * @param paginate
	 */
	public void setPaginate(String paginate) {
		if (null != paginate && paginate.trim().equalsIgnoreCase("false")) {
			this.setPaginate(false);
		} else {
			this.setPaginate(true);
		}
	}

	public Map<String, String[]> getParameters() {
		return parameters;
	}

	public void setParameters(Map<String, String[]> parameters) {
		this.parameters = parameters;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		if (curPage > 1) {
			this.curPage = curPage;
		}
	}

	public void setCurPage(String curPage) {
		if (null == curPage || curPage.trim().equals("")) {
			return;
		}
		int acurPage = Integer.parseInt(curPage);
		this.setCurPage(acurPage);
	}

	public int getPageRecord() {
		return pageRecord;
	}

	public void setPageRecord(int pageRecord) {
		this.pageRecord = pageRecord;
	}

	public void setPageRecord(String pageRecord) {
		if (null != pageRecord && !pageRecord.trim().equals("")) {
			this.pageRecord = Integer.parseInt(pageRecord);
		}
	}

	public int getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}

	/**
	 * 返回下一页页码
	 * 
	 * @return
	 */
	public int getNextPageNumber() {
		int nextPageNumber = this.curPage + 1;
		if (nextPageNumber > this.getMaxPageNumber()) {
			nextPageNumber = this.getMaxPageNumber();
		}
		return nextPageNumber;
	}

	/**
	 * 返回上一页页码
	 * 
	 * @return
	 */
	public int getPreviousPageNumber() {
		int previousPageNumber = this.curPage - 1;
		if (previousPageNumber < 1) {
			previousPageNumber = 1;
		}
		return previousPageNumber;
	}

	/**
	 * 返回最大页码编号
	 * 
	 * @return
	 */
	public int getMaxPageNumber() {
		int maxPageNumber = 1;
		if (this.totalRecord > this.pageRecord) {
			if (0 == this.totalRecord % this.pageRecord) {
				maxPageNumber = this.totalRecord / this.pageRecord;
			} else {
				maxPageNumber = this.totalRecord / this.pageRecord + 1;
			}
		}
		return maxPageNumber;
	}

	/**
	 * 获取起始记录下标
	 * 
	 * @return
	 */
	public int getStartRecordIndex() {
		return this.pageRecord * (this.curPage - 1);
	}

	/**
	 * 获取结束记录下标
	 * 
	 * @return
	 */
	public int getEndRecordIndex() {
		int end = this.pageRecord * this.curPage - 1;
		if (end > (this.totalRecord - 1)) {
			end = (this.totalRecord - 1);
		}
		return end;
	}

	public String toString() {
		// 保存请求的参数到一个表单对象中,以供下次查询使用
		StringBuffer bf = new StringBuffer();
		bf.append("<form name='pageBeanForm' action='" + this.getUrl()
				+ "' method='post'>");
		bf.append("<input type='hidden' name='curPage' value=''/>");// 当前页是目前是空,但在点击分页按钮时会进行设置
		Map<String, String[]> parameters = this.getParameters();// 请求中的所有参数
		String paramName = null;
		String[] paramValues = null;
		Iterator it = parameters.keySet().iterator();
		while (it.hasNext()) {
			paramName = (String) it.next();// 参数名
			if ("curPage".equals(paramName)) {
				continue;// 把当前页参数忽略掉
			}
			paramValues = parameters.get(paramName);// 参数值
			for (int i = 0; null != paramValues && i < paramValues.length; i++) {
				bf.append("<input type='hidden' name='" + paramName
						+ "' value='" + paramValues[i] + "'/>");
			}
		}
		bf.append("</form>");

		// 显示分页按钮
		bf.append("每页 " + this.getPageRecord() + " 行");
		bf.append(" 共 " + this.getTotalRecord() + " 行");
		bf.append(" 第 " + this.getCurPage() + " 页");
		bf.append(" 共 " + this.getMaxPageNumber() + " 页");
		bf.append(" <a href='javascript:gotoPage(" + 1 + ")'>首页</a>");
		bf.append(" <a href='javascript:gotoPage("
				+ this.getPreviousPageNumber() + ")'>上一页</a>");
		bf.append(" <a href='javascript:gotoPage(" + this.getNextPageNumber()
				+ ")'>下一页</a>");
		bf.append(" <a href='javascript:gotoPage(" + this.getMaxPageNumber()
				+ ")'>尾页</a>");
		bf.append(" 页数 <input type='text' id='pageNumber'  style='width:20px;' /> <a href='javascript:jumpPage()'>GO</a>");

		// 动态生成js
		bf.append("<script>");
		bf.append("function gotoPage(curPage){document.pageBeanForm.curPage.value=curPage;document.pageBeanForm.submit();}");
		bf.append("var maxPageNumber =" + this.getMaxPageNumber() + ";");
		bf.append("function jumpPage(){var curPage=document.getElementById('pageNumber').value;if(curPage==''||isNaN(curPage)||parseInt(curPage)<1||parseInt(curPage)>maxPageNumber){alert('请输入一个正确的页码');return;}gotoPage(curPage);}");
		bf.append("</script>");
		return bf.toString();
	}
}
发表评论
用户名: 匿名