⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pageutil.java

📁 Struts+Hibernate实现软件缺陷的跟踪管理
💻 JAVA
字号:
package xing.five.util;

public class PageUtil {
	private final static int DEF_PAGE_SIZE = 5;// 默认当前页的容量

	/**
	 * 功能:传入查询前初始化的page实例,创建一个新的Page实例
	 * 
	 * @param page
	 * @param totalRow
	 * @return
	 */
	public static Page createPage(Page page, int totalRow) {
		return createPage(page.getUrl(), page.getParam(), page.getPageSize(),
				page.getCurPage(), totalRow);
	}

	public static Page createPage(String url, String param, int pageSize,
			int curPage, int totalRow) {
		pageSize = getpageSize(pageSize);
		curPage = getcurPage(curPage);
		int beginIndex = getBeginIndex(pageSize, curPage);
		int totalPage = getTotalPage(pageSize, totalRow);
		boolean hasNextPage = hasNextPage(curPage, totalPage);
		boolean hasPrePage = hasPrePage(curPage);
		String pageToolBar = getPageToolBar(url, param, hasPrePage,
				hasNextPage, pageSize, totalPage, totalRow, curPage);
		return new Page(hasPrePage, hasNextPage, pageSize, totalPage, totalRow,
				curPage, beginIndex, pageToolBar);
	}

	private static String getPageToolBar(String url, String param,
			boolean hasPrePage, boolean hasNextPage, int pageSize,
			int totalPage, int totalRow, int curPage) {
		StringBuffer strBuf = new StringBuffer();

		boolean isHaveParam = false;

		if (null != param && !"".equals(param)) {
			isHaveParam = true;
		}

		strBuf.append("当前第" + curPage + "页/共" + totalPage + "页  总记录" + totalRow
				+ "条");
		if (hasPrePage) {
			strBuf.append("  <a href=\"" + url + "?page=1"
					+ ((isHaveParam) ? "&" + param : "") + "\">首页</a>");
			strBuf.append("  <a href=\"" + url + "?page=" + (curPage - 1)
					+ ((isHaveParam) ? "&" + param : "") + "\">上一页</a>");
		} else {
			strBuf.append("  首页  上一页");
		}
		if (hasNextPage) {
			strBuf.append("  <a href=\"" + url + "?page=" + (curPage + 1)
					+ ((isHaveParam) ? "&" + param : "") + "\">下一页</a>");
			strBuf.append("  <a href=\"" + url + "?page=" + totalPage
					+ ((isHaveParam) ? "&" + param : "") + "\">尾页</a>");
		} else {
			strBuf.append("  下一页  尾页");
		}
		return strBuf.toString();
	}

	private static int getpageSize(int pageSize) {
		return pageSize == 0 ? DEF_PAGE_SIZE : pageSize;
	}

	private static int getcurPage(int curPage) {
		return curPage == 0 ? 1 : curPage;
	}

	private static int getBeginIndex(int pageSize, int curPage) {
		return (curPage - 1) * pageSize;
	}

	private static int getTotalPage(int pageSize, int totalRow) {
		int totalPage = 0;
		if (totalRow % pageSize == 0)
			totalPage = totalRow / pageSize;
		else
			totalPage = totalRow / pageSize + 1;
		return totalPage;
	}

	private static boolean hasPrePage(int curPage) {
		return curPage == 1 ? false : true;
	}

	private static boolean hasNextPage(int curPage, int totalPage) {
		return curPage == totalPage || totalPage == 0 ? false : true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -