日期:2014-05-16  浏览次数:20725 次

struts2+ibatis+mysql分页实现

把自己项目里的分页总结一下:

首先写了一个Pagination类

public class Pagination {

	/**
	 * 生成分页信息 包括第一页,上一页,下一页,最后一页等
	 * 
	 * @param pageNum
	 *            当前页数
	 * @param pageCount
	 *            总页数
	 * @param recordCount
	 *            总记录数
	 * @param pageUrl
	 *            页面URL
	 * @return
	 */
	public static String getPagination(int pageNum, int pageCount,
			int recordCount, String pageUrl) {

		if(pageCount <= 1){
			return "";
		}
		
		if (pageUrl == null || pageUrl.equals("")) {
			throw new RuntimeException("pageUrl is null or blank String");
		}
		
		String url = pageUrl.contains("?") ? pageUrl : pageUrl + "?";
	
		StringBuffer buffer = new StringBuffer();

		buffer.append("第  " + pageNum + "/" + pageCount + " 页  共  "
				+ recordCount + " 条记录  ");
		buffer.append(pageNum == 1 ? "第一页" : "<a href='" + url
				+ "&pageNum=1'>第一页</a>");
		buffer.append("&nbsp;&nbsp;");
		buffer.append(pageNum == 1 ? "上一页" : "<a href='" + url + "&pageNum="
				+ (pageNum - 1) + "'>上一页</a>");
		buffer.append("&nbsp;&nbsp;");
		buffer.append(pageNum == pageCount ? "下一页" : "<a href='" + url
				+ "&pageNum=" + (pageNum + 1) + "'>下一页</a>");
		buffer.append("&nbsp;&nbsp;");
		buffer.append(pageNum == pageCount ? "最后一页" : "<a href='" + url
				+ "&pageNum=" + pageCount + "'>最后一页</a>");
		buffer.append("&nbsp;&nbsp;&nbsp;&nbsp;");
		buffer
				.append("到<input type='text' name='goto_input' style='width:25px;font-size:12px;text-align:center' />页");
		buffer
				.append("<input type='button' name='goto_button' style='width:25px;font-size:12px' value='Go' />");

		buffer.append("<script type='text/javascript'>");
		buffer.append("    function helloweenvsfei_enter(){");

		buffer.append("    	  if(event.keyCode == 13){");
		buffer.append("    		  helloweenvsfei_goto();");
		buffer.append("    		  return false;");
		buffer.append("       }");
		buffer.append("    	  return true;");
		buffer.append("    }");

		buffer.append("function helloweenvsfei_goto(){");
		buffer.append("    var numText = document.getElementsByName('goto_input')[0].value;");
		buffer.append("    var num = parseInt(numText, 10);");
		buffer.append("    if(!num){");
		buffer.append("        alert('页数必须是数字');");
		buffer.append("        return;");
		buffer.append("    }");

		buffer.append("    if(num < 1 || num >" + pageCount + "){");
		buffer.append("        alert('页数必须大于0,且小于总页数" + pageCount + "');");
		buffer.append("        return;");
		buffer.append("    }");
		buffer.append("    location ='" + url + "&pageNum='+num;");
		buffer.append("}");

		buffer.append("document.getElementsByName('goto_input')[0].onkeypress = helloweenvsfei_enter;");
		buffer.append("document.getElementsByName('goto_button')[0].onclick = helloweenvsfei_goto;");
		
		buffer.append("</script>");
		
		return buffer.toString();
	}

}

?在action中定义变量,生成get,set方法

?

private int pageNum = 1;
	private String pagination = null;
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public String getPagination() {
		return pagination;
	}
	public void setPagination(String pagination) {
		this.pagination = pagination;
	}

?主体类

public String list(){
		authNews=new AuthNews();
		authNews.setType(Constants.PRODUCT_FILE_TYPE[0]);

		int recordCount = authNewsService.getCount(authNews);
		PageBean pageBean = new PageBean(pageNum, 2, recordCount);

		authNews.setPageBean(pageBean);
		try {
			listAuthNews=authNewsService.getAuthNewsByType(authNews);
		} catch (Exception e) {
			e.printStackTrace();
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageUrl = request.getContextPath()
				+ "/problemListAction.html";
		pagination = Pagination.getPagi