日期:2014-05-16 浏览次数:20768 次
发表一个自己常用的分页的通用代码,实现了ajax分页。此例子是在Struts2+Hibernate+Spring框架下实现的,分页工具类是通用的,只不过查询、控制是根据使用的方法的不同而不同。
?
PaginationUtil.java
package com.dsh.pagination.tool;
/**
 * ajax分页
 * @author: DuanLang
 * @company:oddTech
 * @time:2011-12-2 上午11:10:36
 */
public class PaginationUtil {
	/** 设置当前页码 */
	private int curPage = 1;
	/** 设置每一页的行数 */
	private int pageSize = 15;
	/** 从数据库读取的开始记录数 */
	private int start;
	/** 从数据库读取的行数(每一页显示的记录数) */
	private int pageCount;
	/** 总共行数(记录数) */
	private int totalRow;
	/** 总共页数 */
	private int totalPage;
	/** 分页导航条 */
	private String pageBar;
	/**
	 * 
	 * 根据从数据库读出的总记录数初始化相应的分页变量
	 * 
	 * @param totalRow
	 *            总记录数
	 */
	public void setPagesVariable(int totalRow) {
		this.setTotalRow(totalRow);
		this.setTotalPage(totalRow / pageSize);
		if (totalRow % pageSize > 0) {
			this.setTotalPage(totalPage + 1);
		}
		if (curPage > 1) {
			this.setStart((curPage - 1) * pageSize);
		} else {
			this.setStart(0);
		}
		this.setPageCount(pageSize);
	}
	/**
	 * 
	 * @return the curPage
	 */
	public int getCurPage() {
		return curPage;
	}
	/**
	 * 
	 * @param curPage
	 * 
	 *            the curPage to set
	 */
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	/**
	 * 
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}
	/**
	 * 
	 * @param pageSize
	 * 
	 *            the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	/**
	 * 
	 * @return the start
	 */
	public int getStart() {
		return start;
	}
	/**
	 * 
	 * @param start
	 * 
	 *            the start to set
	 */
	public void setStart(int start) {
		this.start = start;
	}
	/**
	 * 
	 * @return the pageCount
	 */
	public int getPageCount() {
		return pageCount;
	}
	/**
	 * 
	 * @param pageCount
	 *            the pageCount to set
	 */
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	/**
	 * 
	 * @return the totalRow
	 */
	public int getTotalRow() {
		return totalRow;
	}
	/**
	 * 
	 * @param totalRow
	 * 
	 *            the totalRow to set
	 */
	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;
	}
	/**
	 * 
	 * @return the totalPage
	 */
	public int getTotalPage() {
		return totalPage;
	}
	/**
	 * 
	 * @param totalPage
	 * 
	 *            the totalPage to set
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	/**
	 * 
	 * @return the pageBar
	 */
	public String getPageBar() {
		return pageBar;
	}
	/**
	 * 
	 * @param pageBar
	 * 
	 *            the pageBar to set
	 */
	public void setPageBar(String pageBar) {
		this.pageBar = pageBar;
	}
	/**
	 * 
	 * 分页导航条(显示分页链接控制代码)
	 * 
	 * @return
	 */
	public String getToolsMenu() {
		StringBuffer str = new StringBuffer("");
		int next, prev;
		prev = curPage - 1;
		next = curPage + 1;
		if (curPage > 1) {
			str.append("<a href=\"#\" onclick=\"this.parentNode.getElementsByTagName('input')[0].value=1;this.parentNode.submit();\">首页</a> ");
		} else {
			str.append("首页 ");
		}
		if (curPage > 1) {
			str.append("<a href=\"#\" onclick=\"this.parentNode.getElementsByTagName('input')[0].value="
					+ prev + ";this.parentNode.submit();\">上页</a> ");
		} else {
			str.append("上页</a> ");
		}
		if (curPage < totalPage) {
			str.append("<a href=\"#\" onclick=\"this.parentNode.getElementsByTagName('input')[0].value="
					+ next + ";this.parentNode.submit();\">下页</a> ");
		} else {
			str.append("下页 ");
		}
		if (totalPage > 1 && curPage != totalPage) {
			str.append("<a href=\"#\"  onclick=\"this.parentNode.getElementsByTagName('input')[0].value="
					+ totalPage
					+ ";this.parentNode.submit();\">末页</a>  ");
		} else {