JSP自定义分页标签TAG

首先我们需要在WEB-INF/tld/目录下创建page.tld文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<taglib>
	<tlib-version>1.0</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>page</short-name>
	<uri>http://www.kevin.com/taglibs/page</uri>
	<tag>
		<name>htmlPage</name>
		<tag-class>com.lss.common.tag.PageTag</tag-class>
		<body-content>JSP</body-content>
		<description>htmlPage Tag</description>
		<attribute>
			<name>pageNo</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>pageSize</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>totalSum</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>url</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>showPage</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
</taglib>

package com.lss.common.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.lss.common.page.HtmlPage;
import com.lss.common.page.Page;
import com.lss.common.page.VariablePage;

/**
 * 分页处理标签
 *
 * @description
 * @DATE 2012-3-11下午09:08:46
 */
public class PageTag extends TagSupport {
	private int pageNo;
	private int pageSize = 10;
	private int totalSum;
	private int showPage = 10;
	private String url;

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalSum() {
		return totalSum;
	}

	public void setTotalSum(int totalSum) {
		this.totalSum = totalSum;
	}

	public int getShowPage() {
		return showPage;
	}

	public void setShowPage(int showPage) {
		this.showPage = showPage;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public int doEndTag() throws JspException {
		if (pageSize == 0) {
			return TagSupport.SKIP_PAGE;// 不显示分页
		} else if (pageSize > totalSum) {
			return TagSupport.SKIP_BODY;// 不显示分页
		}
		JspWriter out = pageContext.getOut();
		try {
			if (url.indexOf(";jsessionid=") == -1) {
				if (url.indexOf("?") == -1) {
					url += ";jsessionid=" + pageContext.getSession().getId() + "?pageNo=";
				} else {
					url = url.replaceFirst("\\?", ";jsessionid=" + pageContext.getSession().getId() + "?");
					if (url.indexOf("pageNo=") == -1) {
						url += "&pageNo=";
					}
				}
			}
			url = pageContext.getServletContext().getContextPath() + url;
			VariablePage variablePage = new VariablePage(pageNo, totalSum, pageSize, url);
			variablePage.setShowPageNum(showPage);
			Page page = new HtmlPage(variablePage);
			out.print(page.getFirstNo() + "  " + page.getBackpageNum() + "  " + page.pagination() + "  " + page.getNextpageNum() + "  " + page.getLastNo());
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return super.doEndTag();
	}

	@Override
	public void release() {
		url = null;
		pageNo = 0;
		totalSum = 0;
		pageSize = 10;
		super.release();
	}

	private static final long serialVersionUID = -2642907859641024483L;
}
/**
 * HTML 分页类
 *
 * @description
 * @DATE 2012-3-11下午10:33:14
 */
public class HtmlPage extends Page {
	public HtmlPage(VariablePage variablePage) {
		super(variablePage);
	}

	public String pagination() {
		String printNo = "";
		// 如果已分页;并且页数小于等于要显示的页数
		if (variablePage.totalPage > 1 && variablePage.totalPage <= variablePage.showPageNum) {
			return displayAll();
			// 如果分页数:大于显示的页码数
		} else if (variablePage.totalPage > 1 && variablePage.totalPage > variablePage.showPageNum) {
			if (variablePage.pageNo == 1) {// 当前页等于第一页
				return fromFirstPagePrint();
			} else if (variablePage.pageNo == variablePage.totalPage) {// 当前页等于最后一页
				return fromLastPagePrint();
			} else {// 如果当前页:即不是首页也不是尾页
				if (variablePage.showPageNum % 2 == 0) {// 可以平分页码
					int print$No = variablePage.showPageNum / 2;
					if (variablePage.pageNo >= print$No) {
						int index$No = variablePage.pageNo - print$No;
						if (variablePage.pageNo + print$No >= variablePage.totalPage) {
							return fromLastPagePrint();
						} else {
							if (index$No == 0)
								index$No = 1;
							for (int i = index$No; i < (variablePage.showPageNum + index$No); i++) {
								if (i == variablePage.pageNo) {// 如果是当前页:不添加连接URL
									printNo += (i + variablePage.split);
								} else {
									printNo += (buildA(variablePage, i) + variablePage.split);
								}
							}
						}
					} else {
						return fromFirstPagePrint();
					}
				} else {// 打印页数不是偶数时:
					int print$No = variablePage.showPageNum / 2 + 1;
					if (variablePage.pageNo >= print$No && variablePage.pageNo + print$No < variablePage.totalPage) {
						int index$No = variablePage.pageNo - print$No + 1;
						for (int i = index$No; i < variablePage.showPageNum + index$No; i++) {
							if (i == variablePage.pageNo) {// 如果是当前页:不添加连接URL
								printNo += (i + variablePage.split);
							} else {
								printNo += (buildA(variablePage, i) + variablePage.split);
							}
						}
					} else if (variablePage.pageNo <= print$No) {// 从第一页开始
						return fromFirstPagePrint();
					} else {
						return fromLastPagePrint();
					}
				}
			}
			return (printNo);
		} else {
			return "1";
		}
	}

	public String getBackpageNum() {
		if (variablePage.pageNo <= 1) {
			return buildSpan("上一页", variablePage);
		} else {
			return buildA("上一页", (variablePage.url + (variablePage.pageNo - 1)));
		}
	}

	public String getNextpageNum() {
		if (variablePage.pageNo >= variablePage.totalPage) {
			return buildSpan("下一页", variablePage);
		} else {
			return buildA("下一页", variablePage.url + (variablePage.pageNo + 1));
		}
	}

	public String buildSpan(String text, VariablePage variablePage) {
		return "<span style=\"color:gray;\">" + text + "</span>";
	}

	public String buildA(String text, String url) {
		return "<a href=\"" + url + "\">" + text + "</a>";
	}

	public String buildA(VariablePage variablePage, int num) {
		return ("<a href=\"" + variablePage.url + num + "\">" + num + "</a>");
	}
}
public abstract class Page {
	protected VariablePage variablePage;

	public Page(VariablePage variablePage) {
		this.variablePage = variablePage;
		calculateTotalPage();
	}

	public int getStartIndex() {
		return (getValidpageNum() - 1) * variablePage.pageSize;
	}

	public String getBackpageNum() {
		if (variablePage.pageNo <= 1) {
			return buildSpan("上一页", variablePage);
		} else {
			return buildA("上一页", variablePage.getUrl() + (variablePage.pageNo - 1));
		}
	}

	public String getNextpageNum() {
		if (variablePage.pageNo >= variablePage.totalPage) {
			return buildSpan("下一页", variablePage);
		} else {
			return buildA("下一页", variablePage.getUrl() + (variablePage.pageNo + 1));
		}
	}

	/**
	 * 计算总页数
	 */
	private void calculateTotalPage() {
		if (variablePage.totalSum % variablePage.pageSize == 0) {
			variablePage.totalPage = variablePage.totalSum / variablePage.pageSize;
		} else {
			variablePage.totalPage = variablePage.totalSum / variablePage.pageSize + 1;
		}
		if (variablePage.totalPage < variablePage.pageNo) {
			variablePage.pageNo = variablePage.totalPage;
		} else if (variablePage.pageNo < 1) {
			variablePage.pageNo = 1;
		}
	}

	protected String displayAll() {
		StringBuilder sBuilder = new StringBuilder(10);
		sBuilder.append(variablePage.split);
		for (int i = 1; i <= variablePage.totalPage; i++) {
			if (i == variablePage.pageNo) {
				sBuilder.append(i + variablePage.split);
			} else {
				sBuilder.append(buildA(variablePage, i) + variablePage.split);
			}
		}
		return sBuilder.toString();
	}

	/**
	 * 抽象出分页方法
	 *
	 * @return
	 */
	public abstract String pagination();

	/**
	 * 实现从第一页开始打印
	 *
	 * @return
	 */
	protected final String fromFirstPagePrint() {
		StringBuffer buffer = new StringBuffer(100);
		for (int i = 1; i <= variablePage.showPageNum; i++) {
			if (i == variablePage.pageNo) {// 如果是当前页:不添加连接URL
				buffer.append(i + variablePage.split);
			} else {
				buffer.append(buildA(variablePage, i) + variablePage.split);
			}
		}
		return buffer.toString();
	}

	/**
	 * 实现从最后一页开始打印
	 *
	 * @return
	 */
	protected final String fromLastPagePrint() {
		StringBuffer buffer = new StringBuffer(100);
		int startPage = variablePage.totalPage - (variablePage.showPageNum - 1);
		for (int i = startPage; i <= variablePage.totalPage; i++) {
			if (i == variablePage.pageNo) {// 如果是当前页:不添加连接URL
				buffer.append(i + variablePage.split);
			} else {
				buffer.append(buildA(variablePage, i) + variablePage.split);
			}
		}
		return buffer.toString();
	}

	public String getFirstNo() {
		if (isExistsPagination()) {
			return buildA("首页", variablePage.url + 1);
		} else {
			return buildSpan("首页", variablePage);
		}
	}

	/**
	 * 判断是否存在分页
	 *
	 * @return
	 */
	private boolean isExistsPagination() {
		if (variablePage.totalSum > 1 && variablePage.totalPage > 1) {
			return true;
		}
		return false;
	}

	public String getLastNo() {
		if (isExistsPagination()) {
			return buildA("尾页", variablePage.url + variablePage.totalPage);
		} else {
			return buildSpan("尾页", variablePage);
		}
	}

	protected int getValidpageNum() {
		if (variablePage.totalPage < variablePage.pageNo) {
			return variablePage.pageNo = variablePage.totalPage;
		} else if (variablePage.pageNo < 1) {
			return variablePage.pageNo = 1;
		} else {
			return variablePage.pageNo;
		}
	}

	public VariablePage getPageContant() {
		return this.variablePage;
	}

	public abstract String buildSpan(String text, VariablePage variablePage);

	public abstract String buildA(String text, String url);

	public abstract String buildA(VariablePage variablePage, int num);

	public String printModifyPageSize(int pageSize){
		StringBuilder builder=new StringBuilder(100);
		builder.append("每页<input type='text' id='pageSize' style='width:20px' maxlength='2' name='pageSize'");
		if(pageSize>0){
			builder.append(" value='"+pageSize+"' ");
		}
		builder.append("/>条");
		return builder.toString();
	}
}
/**
 * 分页基础变量
 *
 * @description
 * @DATE 2012-3-11下午10:34:00
 */
public class SimpleVariable {
	/**
	 * 显示页码
	 */
	protected int showPageNum = 10;
	/**
	 * 连接URL
	 */
	protected String url = null;
	/**
	 * 当前页码
	 */
	protected int pageNo = 1;
	/**
	 * 总页码
	 */
	protected int totalPage = 1;
	/**
	 * 总条数
	 */
	protected int totalSum = 0;
	/**
	 * 每页显示条数
	 */
	protected int pageSize = 10;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalSum() {
		return totalSum;
	}

	public void setTotalSum(int totalSum) {
		this.totalSum = totalSum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getShowPageNum() {
		return showPageNum;
	}

	public void setShowPageNum(int showPageNum) {
		this.showPageNum = showPageNum;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
}
/**
 * 分页变量
 *
 * @description
 * @DATE 2012-3-11下午10:34:08
 */
public class VariablePage extends SimpleVariable {
	protected String split = "  ";
	protected String style = "class='page'";

	public VariablePage(int pageNo, int totalSum, int pageSize, String url) {
		pageNo = pageNo < 1 ? 1 : pageNo;
		super.pageNo = pageNo;
		this.totalSum = totalSum;
		this.url = url;
		this.pageSize = pageSize;
	}

	public VariablePage(int pageNum, int totalSum, String url) {
		pageNum = pageNum < 1 ? 1 : pageNum;
		super.pageNo = pageNum;
		this.totalSum = totalSum;
		this.url = url;
	}

	public String getSplit() {
		return split;
	}

	public void setSplit(String split) {
		this.split = split;
	}

}

#需要使用该分页标签的JSP 使用方法如下:

JSP 头部引入

<%@taglib
uri="http://www.kevin.com/taglibs/page"
prefix="page"
%>

使用以下标签生成分页HTML代码

<page:htmlPage
pageNo="${pageNo}"
url="your_url" 
totalSum="${totalSum}"
showPage="10"
pageSize="12"/>

JSP自定义分页标签TAG,布布扣,bubuko.com

时间: 2024-12-04 16:08:57

JSP自定义分页标签TAG的相关文章

jsp自定义分页标签

1.web工程的整体结构 2.Eclipse中tld文件的构建方法3.编码 package mytags; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; public class PagerTag extends TagSupport { /** * */ private static final long serialV

SSH自定义分页标签

本文参考文章:http://blog.csdn.net/qjyong/article/details/3240303 一.标签处理类: package cn.conris.sys.form; import java.io.IOException; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import java

自定义分页标签步骤

1.引架包 <!--javaee,处理自定义标签--> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> 2.在WEB-INF中创建.tld.文件,加入需引用

JSP自定义业务标签

自定义标签: package cn.hv.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; public class BodyTag extends BodyTagSupport { private St

自定义分页标签儿

这个前面也就是匆匆带过了,还是好好描述一番吧. 什么是自定义标签儿.就是在jsp页面中,我们自己生成定义的标签儿,可以处理一定的逻辑.所以本质上还是要有java代码来处理的. 自定义标签儿能够运行的三个要素: 1.对应的java处理程序. 2.tld标签儿语言定义 3.在.jsp文件中导入包儿,并且,在页面中使用. 那么在3中得到导入路径,通过3知道对应的tld文件,那么一定是在tld中有对应的java包的引入.这样三者构成一种连接关系,那么代码就能执行了. 下面以分页为例,来简单阐述这个自定义

NET Core-TagHelper实现分页标签

1.继承父类TagHelper并重写Process方法(这里还有一个异步的方法ProcessAsync各位可以自己尝试下) 2.注意:怎么在试图页面使用自定义标签 3.注意:怎么识别标签中的属性 4.注意:自定义标签类怎么获取分页参数 5.效果展示 下面一步一个脚印的来分享: 1.继承父类TagHelper并重写Process方法(这里还有一个异步的方法ProcessAsync各位可以自己尝试下) 首先咋们定义一个类取名为PagerTagHelper,这里需要继承TagHelper类,重写Pro

jsp自定义标签分页

自定义标签分页大大简化了jsp页面的代码量,减少了代码的冗余,提高了代码的重用性. 第一步:建立分页实体page类 1 package com.soda.util; 2 /** 3 * @description 分页实体类 4 * @author line 5 * @time 2016年8月28日11:16:50 6 */ 7 public class Page { 8 private int pageSize;//每页记录per 9 private int totalSize;//总记录tot

jsp自定义标签tag EL函数

原文:jsp自定义标签tagEL函数 源代码下载地址:http://www.zuidaima.com/share/1550463459052544.htm 简单易懂的自定义EL函数 tag.java </pre><p> </p><pre name="code" class="java">package com.zuidaima.tag; import java.io.IOException; import javax.

JSP自定义标签

假设 向浏览器输出当前客户的IP地址 ,我们现在可以这样写 <% //获取当前用户的IP地址 String ip=request.getRemoteHost(); out.print("当前用户的IP地址是:"+ip); %> 但我们现在为了要在jsp页面尽量减少java代码,这个时候我们可以向前面学标签一样,自己来定义一个标签,通过调用标签来达到实现显示客户端的ip地址. 首先第一步我们要创建一个普通的java类,继承SimpleTagSupport类,叫标签处理器类(用