Jsp自定义分页标签,很好用的哦

分页标签实现类
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.sinovatech.cms.common.model.entity.PageBean;

/**
 * 分页标签
 */
public class PageTag extends BodyTagSupport {
	/**
	 * 序列号
	 */
	private static final long serialVersionUID = -7267567631843642898L;
	private int sizeArray[]  = {8,10,15,20,25,30,50,100};

	public int doEndTag() throws JspException {
		try {
			HttpServletRequest request = (HttpServletRequest) super.pageContext.getRequest();
			PageBean page =(PageBean) request.getAttribute("page");
			StringBuffer sb = new StringBuffer();
			int showCount=5;
			int showStart=0;
			int showEnd=0;
			if(page.getTotalPage()<=showCount){
				showStart=1;
				showEnd=page.getTotalPage()+1;
			}else{
				if(page.getCurrentPage()<=(showCount+1)/2){
					showStart=1;	
					showEnd=showCount+1;
				}else if(page.getCurrentPage()>=page.getTotalPage()-(showCount-1)/2){
					showStart=page.getTotalPage()-showCount+1;	
					showEnd=page.getTotalPage()+1;
				}else{
					showStart=page.getCurrentPage()-(showCount-1)/2;
					if(page.getCurrentPage()+(showCount-1)/2+1>page.getTotalPage()){
						showEnd = page.getTotalPage();
					}else{
						showEnd = page.getCurrentPage()+(showCount-1)/2+1;
					}
				}
			}
			sb.append(" <div class=\"numbox\">");
			sb.append("<div class=\"pagejump\">");
			//如果改变每页显示数量,则还原当前页为第一页!
			sb.append("每页记录数:<select style=\"width:60px;\" name='pageSize' onchange='goToPage(1)' value=\""+page.getPageSize()+"\"> ");
			//遍历每页显示数量的数组
			for (int i = 0; i < sizeArray.length; i++) {
				if(page.getPageSize()==sizeArray[i]){
					sb.append("<option value='"+sizeArray[i]+"' selected>"+sizeArray[i]+"</option>");
				}else{
					sb.append("<option value='"+sizeArray[i]+"'>"+sizeArray[i]+"</option>");
				}
			}
			sb.append("</select>条");
			sb.append("跳到第<input type=\"text\" id=\"writePage\" value=\""+page.getCurrentPage()+"\" title=\""+page.getCurrentPage()+"\" totalPage=\""+page.getTotalPage()+"\" onblur=\"checkIsNum(this);\" class=\"input whichpage\">页");
			sb.append("<input type=\"submit\" class=\"btn whichbtn\" value=\"go\" onclick='goToPage($(\"#writePage\").val())'/>");
			sb.append("</div>");
			sb.append(" <div class=\"nummenu\">");
			sb.append("<span class=\"num_page\" title='第"+page.getCurrentPage()+"/"+page.getTotalPage()+"页'>第"+page.getCurrentPage()+"/"+page.getTotalPage()+"页 共<b>"+page.getTotalRecords()+"</b>条记录</span>");
			if (page.getTotalPage() == 1) {//只有1页
				sb.append("<a class=\"homepage\" href=\"javascript:goToPage(1);\" title=\"首页\">首页</a>");
				sb.append("<a href=\"javascript:void(0);\" class=\"page-numbers\" title=\"上一页\"><<</a>");
				sb.append("<a href=\"javascript:goToPage(1);\" class=\"page-numbers current\" title=\"当前第1页\">1</a>");
				sb.append("<a href=\"javascript:void(0);\" class=\"page-numbers\" title=\"下一页\">>></a>");
				sb.append("<a href=\"javascript:goToPage(1);\" class=\"endpage\" title=\"末页\">末页</a>");
			} else {//大于1页
				if(page.getCurrentPage()<=1){
					sb.append("<a class=\"homepage\" href=\"javascript:goToPage(1);\" title=\"首页\">首页</a>");
					sb.append("<a href=\"javascript:void(0);\" class=\"page-numbers\" title=\"上一页\"><<</a>");
					for(int i=showStart;i<showEnd;i++){
						if(i==page.getCurrentPage()){
							sb.append("<a href=\"javascript:goToPage("+i+")\" class=\"page-numbers current\" title='当前第"+i+"页'>"+i+"</a>");
						}else{
							sb.append("<a href=\"javascript:goToPage("+i+")\" class=\"page-numbers\" title='第"+i+"页'>"+i+"</a>");
						}
					}
					sb.append("<a href=\"javascript:goToPage("+(page.getCurrentPage()+1)+")\" class=\"page-numbers\" title=\"下一页\">>></a>");
					sb.append("<a href=\"javascript:goToPage("+page.getTotalPage()+")\" class=\"endpage\" title=\"末页\">末页</a>&nbsp;");
				} else if(page.getCurrentPage()>=page.getTotalPage()){
					sb.append("<a class=\"homepage\" href=\"javascript:goToPage(1);\" title=\"首页\">首页</a>");
					sb.append("<a href=\"javascript:goToPage("+(page.getCurrentPage()-1)+")\" class=\"page-numbers\" title=\"上一页\"><<</a>");
					for(int i=showStart;i<showEnd;i++){
						if(i==page.getCurrentPage()){
							sb.append("<a href=\"javascript:goToPage("+i+")\" class=\"page-numbers current\" title='当前第"+i+"页'>"+i+"</a>");
						}else{
							sb.append("<a href=\"javascript:goToPage("+i+")\" class=\"page-numbers\" title='第"+i+"页'>"+i+"</a>");
						}
					}
					sb.append("<a href=\"javascript:void(0);\" class=\"page-numbers\" title=\"下一页\">>></a>");
					sb.append("<a href=\"javascript:void(0);\" class=\"endpage\" title=\"末页\">末页</a>");
				}else if(page.getCurrentPage()<page.getTotalPage() && page.getCurrentPage()>1){
					sb.append("<a href=\"javascript:goToPage(1);\" class=\"homepage\" title=\"首页\">首页</a>");
					sb.append("<a href=\"javascript:goToPage("+(page.getCurrentPage()-1)+")\" class=\"page-numbers\" title='上一页'><<</a>");
					for(int i=showStart;i<showEnd;i++){
						if(i==page.getCurrentPage()){
							sb.append("<a href=\"javascript:goToPage("+i+")\" class=\"page-numbers current\" title='当前第"+i+"页'>"+i+"</a>");
						}else{
							sb.append("<a href=\"javascript:goToPage("+i+")\" class=\"page-numbers\" title='第"+i+"页'>"+i+"</a>");
						}
					}
					sb.append("<a href=\"javascript:goToPage("+(page.getCurrentPage()+1)+")\" class=\"page-numbers\" title=\"下一页\">>></a>");
					sb.append("<a href=\"javascript:goToPage("+page.getTotalPage()+")\" class=\"endpage\" title=\"末页\">末页</a>");
				}
			}
			sb.append("</div>");
			sb.append("</div>");
			JspWriter out = super.pageContext.getOut();
			out.write(sb.toString());
		} catch (Exception exception) {
			throw new JspException(exception);
		}
		return SKIP_BODY;
	}

}
page类
public class PageBean {
 /** 
 * 当前页
 */
 private int currentPage = 1;
 /** 
 * 每页显示数据条数
 */
 private int pageSize = 10;
 /** 
 * 总记录数
 */
 private int totalRecords;
 /**
 * 总页数
 */
 private int totalPage;
 /**
 * 默认构造方法
 */
 public PageBean(){}
 /**
 * 自定义构造方法1
 * @param pageRequest
 */
 public PageBean (PageRequest pageRequest,int totalRecords){
 this(pageRequest.getCurrentPage(),pageRequest.getPageSize(),totalRecords);
 }
 /**
 * 自定义构造方法2
 * @param currentPage
 * @param totalRecords
 * @param pageSize
 */
 public PageBean (int currentPage,int pageSize,int totalRecords){
 this.currentPage = currentPage;
 this.pageSize = pageSize;
 this.totalRecords = totalRecords;
 }
 /**
 * 自定义构造方法3
 * @param currentPage
 * @param pageSize
 */
 public PageBean (int currentPage,int pageSize){
 this.currentPage = currentPage;
 this.pageSize = pageSize;
 }
 /**所有的页数都可以通过总的记录数除以每页的数目求得*/
 public int getTotalPage(){
 totalPage = totalRecords/pageSize;
 if(totalRecords % pageSize !=0){
 totalPage++;
 }
 if(totalPage==0){
 totalPage = 1;
 }
 return totalPage;
 }
 public void setTotalPage(int totalPage) {
 this.totalPage = totalPage;
 }
 /**如果当前页为1的话,则没有首页*/
 public boolean isHasFirst(){
 if(currentPage == 1){ 
 return false;
 }else{
 return true;
 }
 }
 /**如果当前页等于总页数的话,说明它已经是最后一页了*/
 public boolean isHasLast(){
 if(currentPage>=getTotalPage()){
 return false;
 }else{
 return true;
 }
 }
 /**如果有最后一页的话,就一定有下一页,否则的话,就没有下一页。*/
 public boolean isHasNext(){
 if(isHasLast()){
 return true;
 }else{
 return false;
 }
 }
 /**如果不是第一页,就一定有前一页。*/
 public boolean isHasPrevious(){
 if(isHasFirst()){
 return true;
 }else{
 return false;
 }
 }
 public int getCurrentPage() {
 return currentPage;
 }
 public void setCurrentPage(int currentPage) {
 this.currentPage = currentPage;
 }
 public int getPageSize() {
 return pageSize;
 }
 public void setPageSize(int pageSize) {
 this.pageSize = pageSize;
 }
 public int getTotalRecords() {
 return totalRecords;
 }
 public void setTotalRecords(int totalRecords) {
 this.totalRecords = totalRecords;
 }
 
}
使用方法:<mm:page/>
忘了前台JS方法了:
var list_form_id = "list_form";
/**跳页的function方法**/
function goToPage(currentPage){
 var $form = $("#"+list_form_id);
 $form.append("<input type='hidden' name='currentPage' value='"+currentPage+"'/>");
 $form.submit();
}
/**
 * 验证输入页数合法性
 * @param _obj
 * @returns {Boolean}
 */
function checkIsNum(_obj){
 var c = new RegExp("^[+]{0,1}[0-9]{1,}$");
 var cp = _obj.value;
 if(cp==""||cp==0){
 alert("请输入正确的页码!");
 _obj.value = _obj.title;
 return;
 }
 if(!c.test(cp)){
 alert("输入页码不合法!");
 _obj.value = _obj.title;
 return;
 }
 if(cp>parseInt($(_obj).attr("totalPage"))){
 alert("页码超过总页数!");
 _obj.value = _obj.title;
 return;
 }
}

你可能感兴趣的:(Jsp自定义分页标签)