您的位置:首页 > Web前端 > JavaScript

jsp自定义分页标签

2014-09-03 16:31 465 查看
首先需要定义一个记录页码相关参数的类:

import java.util.List;

public class Page {

	/** 当前页 */
	private int pageNo;

	/** 每页数据量 */
	private int pageSize;

	/** 总数据量 */
	private int allCount;

	/** 总页数 */
	private int allPage;

	/** 每页开始 */
	private int recordStart;

	/** 每页结束 */
	private int recordEnd;

	/** 是否有上一页 */
	private boolean hasPrePage;

	/** 是否有下一页 */
	private boolean hasNextPage;

	/** 显示页数 */
	private int showCount;

	/** 显示开始 */
	private int showStart;

	/** 显示结束 */
	private int showEnd;
	/**url*/
	private String url;
	
	/**查询条件,可选的*/
	private String queryConditions ;
	
	/**查询出对象分页后的list*/
	private List<Object> list;

	public Page() {
		pageNo = 1;
		pageSize = 0;
		allPage = 1;
		recordStart = 0;
		recordEnd = 0;
		hasPrePage = false;
		hasNextPage = false;
		/*try {
			excecute(); 
		} catch (Exception exception) {
		}
*/	}

	public Page(int pageNo, int allCount) {
		this.pageNo = pageNo;
		this.pageSize = 25;
		this.allCount = allCount;
		this.allPage = 1;
		this.recordStart = 0;
		this.recordEnd = 0;
		hasPrePage = false;
		hasNextPage = false;
		try {
			excecute();
		} catch (Exception exception) {
		}
	}
	public Page(int pageNo, int pageSize, int allCount) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.allCount = allCount;
		this.allPage = 1;
		this.recordStart = 0;
		this.recordEnd = 0;
		hasPrePage = false;
		hasNextPage = false;
		try {
			excecute();
		} catch (Exception exception) {
		}
	}
	public Page(int pageNo, int pageSize, int allCount,List<Object> list) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.allCount = allCount;
		this.allPage = 1;
		this.recordStart = 0;
		this.recordEnd = 0;
		this.list = list;
		hasPrePage = false;
		hasNextPage = false;
		try {
			excecute();
		} catch (Exception exception) {
		}
	}
	public Page(int pageNo, int pageSize, int allCount,String url) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.allCount = allCount;
		this.allPage = 1;
		this.recordStart = 0;
		this.recordEnd = 0;
		this.url=url;
		hasPrePage = false;
		hasNextPage = false;
		try {
			excecute();
		} catch (Exception exception) {
		}
	}
	public Page(int pageNo, int pageSize, int allCount,String url,String queryConditions) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.allCount = allCount;
		this.allPage = 1;
		this.recordStart = 0;
		this.recordEnd = 0;
		this.url=url;
		this.queryConditions=queryConditions;
		hasPrePage = false;
		hasNextPage = false;
		try {
			excecute();
		} catch (Exception exception) {
		}
	}
	public void excecute() {
		if (pageNo <= 0) {
			pageNo = 1;
		}
		recordStart = (pageNo - 1) * pageSize + 1;
		recordEnd = Math.min(recordStart + pageSize, allCount);
		if (allCount % pageSize == 0) {
			allPage = allCount / pageSize;
		} else {
			allPage = allCount / pageSize + 1;
		}
		if (pageNo > 1) {
			hasPrePage = true;
		}
		if (pageNo < allPage) {
			hasNextPage = true;
		}
		if (showCount <= 0) {
			showCount = 9;
		}

		// 设定显示N页,我设定为9个;看效果图
		showCount = Math.min(showCount, allPage);
		if (showCount < 9) {
			showStart = 1;
			showEnd = showCount;
		} else {
			if (pageNo - 3 <= 1) {
				showStart = 1;
			} else {
				showStart = pageNo - 3;
			}
			if (allPage - 3 < pageNo) {
				showEnd = allPage - 3;
			} else {
				if (showStart == 1) {
					showEnd = showStart + 6;
				} else {
					showEnd = pageNo + 3;
				}
			}
		}
	}

	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 getAllCount() {
		return allCount;
	}

	public void setAllCount(int allCount) {
		this.allCount = allCount;
	}

	public int getAllPage() {
		return allPage;
	}

	public void setAllPage(int allPage) {
		this.allPage = allPage;
	}

	public int getRecordStart() {
		return recordStart;
	}

	public void setRecordStart(int recordStart) {
		this.recordStart = recordStart;
	}

	public int getRecordEnd() {
		return recordEnd;
	}

	public void setRecordEnd(int recordEnd) {
		this.recordEnd = recordEnd;
	}

	public boolean isHasPrePage() {
		return hasPrePage;
	}

	public void setHasPrePage(boolean hasPrePage) {
		this.hasPrePage = hasPrePage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public int getShowCount() {
		return showCount;
	}

	public void setShowCount(int showCount) {
		this.showCount = showCount;
	}

	public int getShowStart() {
		return showStart;
	}

	public void setShowStart(int showStart) {
		this.showStart = showStart;
	}

	public int getShowEnd() {
		return showEnd;
	}

	public void setShowEnd(int showEnd) {
		this.showEnd = showEnd;
	}

	public String getUrl() {
		return url;
	}

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

	public String getQueryConditions() {
		return queryConditions;
	}

	public void setQueryConditions(String queryConditions) {
		this.queryConditions = queryConditions;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}
	
}


然后定义一个分页标签类:

public class PageTag extends BodyTagSupport {
	private static final long serialVersionUID = 1L;
	private String name;
	private String url;
	private String styleClass;
	private String queryConditions;

	public int doEndTag() throws JspException {
		try {
			HttpServletRequest request = (HttpServletRequest) super.pageContext
					.getRequest();
			Object obj = request.getAttribute(name);
			if (obj == null) {
				return 0;
			}
			if (!(obj instanceof Page)) {
				return 0;
			}
			Page page = (Page) obj;
			StringBuffer sb = new StringBuffer();
			if (this.getQueryConditions() != null) {
				page.setUrl(this.getUrl() + "?" + this.getQueryConditions());
			} else
				page.setUrl(this.getUrl());

			if (getReadOnly(page.isHasPrePage()).equals("")) {
				sb.append("<li><a href=" + page.getUrl()
						+ (page.getPageNo() - 1) + ">上一页</a></li>");
			} else {
				sb.append("<li " + getReadOnly(page.isHasPrePage())
						+ ">上一页</li>");
			}

			sb.append(" ");
			if (page.getShowStart() > 1) {
				sb.append("... ");
			}
			for (int i = page.getShowStart(); i < page.getPageNo(); i++) {// 当前页之前的页码
				sb.append("<li>" + "<a href=" + page.getUrl() + i + ">" + i
						+ "</a></li> ");
			}
			sb.append("<li " + " class='" + styleClass + "'>"
					+ page.getPageNo() + "</a></li> ");// 当前页
			for (int i = page.getPageNo() + 1; i <= page.getShowEnd(); i++) {// 当前页之后的页码
				sb.append("<li>" + "<a href=" + page.getUrl() + i + ">" + i
						+ "</a></li> ");
			}
			if (page.getPageNo() < page.getAllPage() && page.getAllPage() > 9) {
				sb.append("... ");
			}
			if (page.getPageNo() + 1 >= page.getAllPage()) {
				page.setPageNo(page.getAllPage() - 1);
			}
			if (getReadOnly(page.isHasNextPage()).equals("")) {
				sb.append("<li><a href=" + page.getUrl()
						+ (page.getPageNo() + 1) + ">下一页</a></li>");
			} else {
				sb.append("<li " + getReadOnly(page.isHasNextPage())
						+ ">下一页</li>");
			}
			JspWriter jspwriter = super.pageContext.getOut();
			jspwriter.write(sb.toString());
		} catch (Exception exception) {
			throw new JspException(exception);
		}
		return 0;
	}

	private String getReadOnly(boolean b) {
		if (!b) {// 按钮失效
			// return "class='disableButton'";
			return "class='disablepage'";
		}
		return "";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

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

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getQueryConditions() {
		return queryConditions;
	}

	public void setQueryConditions(String queryConditions) {
		this.queryConditions = queryConditions;
	}
}


需要创建一个tld文件,这是一个标准的XML文件,这个文件中就包含有对自定义标签的声明,声明指出了标签的名字,实现标签的类,标签的属性等信息。当在页面中使用该标签时,web服务器就会从这个文件中找出相对应的标签类,并实例化后执行。

假设为pageTag.tld

<?xml version="1.0" encoding="UTF-8" ?>

<!-- a tab library descriptor -->

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">

<description>

A tag library exercising SimpleTag handlers.

</description>

<tlib-version>1.0</tlib-version>

<short-name>dream</short-name>

<uri>/pageTag</uri>

<tag>

<name>page</name>

<tag-class>分页标签所在的路径</tag-class>

<body-content>empty</body-content>

<attribute>

<!--标签中的属性-->

<name>name</name>

<!--属性名-->

<required>true</required>

<!--是否必须-->

<rtexprvalue>true</rtexprvalue>

<!--表示该自定义标签的某属性的值可以直接指定或者通过动态计算指定-->

</attribute>

<attribute>

<name>url</name>

<required>true</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>styleClass</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>queryConditions</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

</tag>

</taglib>

在页面中使用的时候只需要引入:

<%@ taglib prefix="pg" uri="/pageTag"%>其中这个pageTag是和tld中的uri相对应的

根据自己的需要在action中实例化一个PageInfo对象即可

在jsp页面中采用下面的形式引用分页标签:

<pg:page name="pageInfo"

url=""

styleClass=""

queryConditions="" />

url路径,styleClass是当前页的样式class,queryConditions这个是查询条件,当然这些是要和PageTag这个类配合使用的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: