分页标签

1 编写标签类
package org.springside.modules.tag;

import java.io.IOException;

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

import org.springside.modules.orm.Page;

public class PageTag extends TagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Page page;

	public void setPage(Page page) {
		this.page = page;
	}

	@Override
	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		StringBuffer sb = new StringBuffer("");
		sb.append(
				"<input type='hidden' name='page.pageNo' id='pageNo' value='"
						+ page.getPageNo() + "' />").append(
				"<input type='hidden' name='page.orderBy' id='orderBy' value='"
						+ page.getOrderBy() + "' />").append(
				"<input type='hidden' name='page.order' id='order' value='"
						+ page.getOrder() + "' />").append(
				"<span> 第" + page.getPageNo() + "/" + page.getTotalPages()
						+ "页 </span>");
		if (!page.isHasPre())
			sb.append("<span>首页</span><span class=\"pre\">上一页</span>");
		if (page.isHasPre())
			sb.append("<a href=\"javascript:void()\" id=\"1\">首页</a>").append(
					"<a href=\"javascript:void()\" class=\"pre\" id=\""
							+ page.getPrePage() + "\">上一页</a>");
		if (page.isHasNext())
			sb.append(
					"<a href=\"javascript:void()\" class=\"next\" id=\""
							+ page.getNextPage() + "\">下一页</a>").append(
					"<a href=\"javascript:void()\" id=\""
							+ page.getTotalPages() + "\">末页</a>");
		if (!page.isHasNext())
			sb.append("<span class=\"next\">下一页</span><span>末页</span>");
		sb.append("    当前共有" + page.getTotalCount() + "条记录");

		try {
			if (sb != null)
				out.println(sb);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	@Override
	public int doStartTag() throws JspException {
		return EVAL_PAGE;
	}

}


2 TLD文件 必须放在WEB-INF/tlds文件夹下
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
	<tlib-version>1.0</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>page</short-name>
	<uri>/pagetag</uri> 
	<!--OutputTag-->
	<tag>
		<name>out</name>
		<tag-class>org.springside.modules.tag.PageTag</tag-class>
		<body-content>empty</body-content>
		<attribute>
			<name>page</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
</taglib>


3 页面引用
<%@ taglib prefix="p" uri="/pagetag" %>


<p:out page="${page}"/>



你可能感兴趣的:(java,jsp,Web,servlet,sun)