java分页器

转载自:java分页代码+css样式

下面是我自己修改的,将它写成一个工具类:

public class PageUtils {

	public static final long EDIT_VIDEO_RECORD_NUMBER_PER_PAGE = 1;// 后台编辑视频页面一页最多显示的记录数
	public static final long EDIT_VIDEO_BAR_TERM_NUMBER = 10;// 后台编辑视频的分页条的显示多少个阿拉佰数字让用户点

	// 分页
	/**
	 * 获取分页条 显示样式(1/4页)首页上一页1234下一页尾页
	 * 
	 * @param totalNum
	 *            记录数量
	 * @param currentPage
	 *            当前页
	 * @param pageNumPerPage
	 *            每页显示记录数
	 * @param barTermNum
	 *            如"首页上一页1234下一页尾页"这里有只显示出1-4页,barTermNum就是四,可见的的分页项下标数
	 * @param param	除当前页数与每页的记录数以后的参数
	 * @return
	 */
	public static String getPageBar(long totalNum, long currentPage,
			long pageNumPerPage, long barTermNum , String param) {
		StringBuffer sb = new StringBuffer();
		
		long pageCount = 0; // 页数
	
		if(pageNumPerPage < 1){
			return null;
		}

		sb.append("<input id=\"page\" name=\"page\" type=\"hidden\" />");
		sb.append("<input id=\"pageNum\" name=\"pageNum\" type=\"hidden\" />");
		
		String[] parametter;
		if(param != null){//传进来的参数也加进去
			parametter = param.split("&");
			
			for (String eachParam : parametter) {
				if(parametter == null){
					continue;
				}
				String[] keyValue = eachParam.split("=");
				if(keyValue != null && keyValue.length == 2){
					sb.append("<input id=\""+ keyValue[0] + "\" name=\"" + keyValue[0] + "\" value=\"" + keyValue[1]+ "\" type=\"hidden\" />");
				}
			}
		}
		
		
		sb.append("<script>" + "function fenye(i,j){"
				+ " document.getElementById('page').value = i;"
				+ " document.getElementById('pageNum').value = j;"
				+ " for(var i=0;;i++){"
				+ "  if(document.forms[i].page != null){"
				+ "    document.forms[i].submit();" + "    break;" + "  }"
				+ " }" + "}" 
				+ " function goPage(j){pn = document.getElementById('go_page_number').value;fenye(pn,j);}"
				+ "</script>");
		
		// 计算分页总数
		pageCount = totalNum / pageNumPerPage + (totalNum % pageNumPerPage == 0 ? 0 : 1);
		// 当前页出局,跳转到首页
		if (currentPage < 1) {
			currentPage = 1;
		}
		else if(currentPage > pageCount){//尾页
			currentPage = pageCount;
		}

		// 显示形如:(1/4页)
		sb.append("<span>(" + currentPage + "/" + pageCount + "页)</span>");

		// 显示-首页
		if (currentPage >= 1) {
			sb.append("<a href=\"javascript:void(0);\" onclick=\"fenye('1','"
					+ pageNumPerPage + "');\">首页</a>");
		}

		// 显示-上一页
		if (currentPage == 1) {
			sb.append("<a href=\"javascript:void(0);\" onclick=\"fenye('1','"
					+ pageNumPerPage + "');\">上一页</a>");
		} else {
			sb.append("<a href=\"javascript:void(0);\" onclick=\"fenye('"
					+ (currentPage - 1) + "','" + pageNumPerPage + "');\">上一页</a>");
		}
		
		// 显示-1234
		long half = barTermNum / 2;//为了让点击的页码尽量显示在中间
		long i = currentPage - half > 0 ? currentPage - half : 1;
		
		for (int j = 1; j <= barTermNum && i <= pageCount; i++, j++) {
			
			if (currentPage == i) {//使用on显示当前所处的页码
				sb.append("<a class=\"on\" href=\"javascript:void(0);\" onclick=\"fenye('"
								+ i + "','" + pageNumPerPage + "');\" >" + i + "</a>");
			} else {
				sb.append("<a href=\"javascript:void(0);\" onclick=\"fenye('"
						+ i + "','" + pageNumPerPage + "');\">" + i + "</a>");
			}
		}
		
		// 显示-下一页
		if (pageCount <= 1) {
			sb.append("<a href=\"javascript:void(0);\" onclick=\"fenye('1','"
					+ pageNumPerPage + "');\">下一页</a>");
		} else if (currentPage == pageCount) {
			sb.append("<a href=\"javascript:void(0);\" onclick=\"fenye('"
					+ (pageCount) + "','" + pageNumPerPage + "');\">下一页</a>");
		} else {
			sb.append("<a href=\"javascript:void(0);\" onclick=\"fenye('"
					+ (currentPage + 1) + "','" + pageNumPerPage + "');\">下一页</a>");
		}
		// 显示-尾页
		if (pageCount <= 1) {
			sb.append("<a href=\"javascript:void(0);\" onclick=\"fenye('1','"
					+ pageNumPerPage + "');\">尾页</a>");
		} else if (currentPage == pageCount) {
			sb.append("<a href=\"javascript:void(0);\" onclick=\"fenye('"
					+ (pageCount) + "','" + pageNumPerPage + "');\">尾页</a>");
		} else {
			sb.append("<a href=\"javascript:void(0);\" onclick=\"fenye('"
					+ (pageCount) + "','" + pageNumPerPage + "');\">尾页</a>");
		}
		
		sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;转<input type='text' size='2' name='go_page_number' id='go_page_number'/>页");
		sb.append("<input type='button' value='GO' onclick=\"goPage('" + pageNumPerPage + "');\"/>");
		
		return sb.toString();
	}

	public static void main(String[] argv) {
		// 测试数据,实际使用时,使用数据库查出总记录数total,当前页currentPage是从页面传过来的变量值
		long total = 100;
		long currentPage = 1;
		String pageBar = PageUtils.getPageBar(total, currentPage,
				PageUtils.EDIT_VIDEO_RECORD_NUMBER_PER_PAGE,//可以分离在常量类里
				PageUtils.EDIT_VIDEO_BAR_TERM_NUMBER,null);
		
		System.out.println(pageBar);
	}
}
 

css还是用转载的文章,我自己不怎么会css和js

.page {
 clear:both;
 padding:10px 0;
 line-height:19px;
 height:19px;
}
.page span,.page a {
 float:left;
 margin-left:5px;
 color:#666;
}
.page .lis {
 float:right;
}
.page a {
 border:1px solid #cccccc;
 padding:0px 7px;
}
.page .on {
 background:#c30101;
 color:#fff;
 border:1px solid #c30101;
}

 这个与servlet分开,很好地应用到自己的controllor类

猜你喜欢

转载自hz-chenwenbiao-rr.iteye.com/blog/1623056