`
myhongkongzhen
  • 浏览: 93885 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

类似GOOGLE,BAIDU的WEB应用PAGE分页标签JAVA实现

    博客分类:
  • J2EE
阅读更多

 

 

2009年5月9号  星期六  天气晴

 

以下是应用JSP标签实现的WEB应用的分页功能的代码

 

 

 核心代码如下:

 

Tag代码:

package g.cms.web.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * @author Jane(吴贞贞)
 * @email myhongkongzhen@gmail.com
 * @since JDK 1.6
 * @alter 2009年5月9号
 * @version 1.0 2009年3月**号
 */
public class PageForCMSTag2 extends BodyTagSupport {
	private static final long serialVersionUID = -4247241487535299862L;

	private String indexColor = "";

	private String countColor = "";

	// private String navigationUrl = "";
	private int pageSize = 10;// 每页显示记录个数

	private int pageIndex = 0;
	private String action;// 请求路径

	// private int pageCount;

	// 自定义参数
	// private String params = "";
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	/**
	 * getPageCount()返回总页数
	 */
	protected int getPageCount() {
		return getRecordCount() % getPageSize() != 0 ? (getRecordCount() / getPageSize()) + 1
				: (getRecordCount() / getPageSize());
	}

	public int getPageIndex() {
		String pi = this.pageContext.getRequest().getParameter("pageIndex");
		if (null == pi || ("").equals(pi.trim())) {
			pageIndex = 0;
		} else {
			pageIndex = Integer.parseInt(pi);
		}
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	private void firstPage(JspWriter out) throws IOException {
		if (getPageIndex() == 0) {
			out.println("<span>[首页]</span>");
		} else {
			out.println("<a href='" + goToPageURI(0) + "'>[首页]</a>");
		}
	}

	private void lastPage(JspWriter out) throws IOException {
		if (getPageIndex() == getPageCount() - 1) {
			out.println("<span>[末页]</span>");
		} else {
			out.println("<a href='" + goToPageURI(getPageCount() - 1)
					+ "'>[末页]</a>");
		}
	}

	private void previousPage(JspWriter out) throws IOException {
		if (getPageIndex() < 1) {
			out.println("<span>[上一页]</span>");
		} else {
			out.println("<a href='" + goToPageURI(getPageIndex() - 1)
					+ "'>[上一页]</a>");
		}
	}

	private void nextPage(JspWriter out) throws IOException {
		if (getPageIndex() >= getPageCount() - 1) {
			out.println("<span>[下一页]</span>");
		} else {
			out.println("<a href='" + goToPageURI(getPageIndex() + 1)
					+ "'>[下一页]</a>");
		}
	}

	@Override
	public int doStartTag() throws JspException {
		// 获取url
		// 设置显示页数的颜色
		getColor();
		StringBuffer strb = new StringBuffer();
		JspWriter out = pageContext.getOut();
		try {
			// 分页
			int front = getPageIndex() - 5; // 前面一截
			int back = getPageIndex() + 5; // 后面一截
			out.println("共" + getPageCount() + "页 &nbsp;");
			out.println("当前第 <font color=" + indexColor + ">"
					+ (getPageIndex() + 1) + "</font>页");
			firstPage(out);
			previousPage(out);
			if (getPageIndex() < 5) // 如果索引在前5页
			{
				if (getPageCount() <= 10) // 总页数不够10页
				{
					for (int i = 0; i < getPageCount(); i++) {
						if (i == getPageIndex())
							strb.append(" " + (getPageIndex() + 1) + " ");
						else
							strb.append("<a href=" + goToPageURI(i) + ">["
									+ (i + 1) + "]</a>");
					}
				} else {
					for (int i = 0; i < 9; i++) {
						if (i == getPageIndex())
							strb.append(" " + (getPageIndex() + 1) + " ");
						else
							strb.append("<a href=" + goToPageURI(i) + ">["
									+ (i + 1) + "]</a>");
					}
				}
			} else {
				if (front >= 1) {
					for (int i = 4; i > 0; i--)
						strb.append("<a href="
								+ goToPageURI(getPageIndex() - i) + ">["
								+ (getPageIndex() - (i - 1)) + "]</a>");
				} else {
					for (int i = 1; i < getPageIndex(); i++)
						strb.append("<a href=" + goToPageURI(i) + ">["
								+ (i + 1) + "]</a>");
				}

				strb.append(" " + (getPageIndex() + 1) + " "); // 分界线 eg: 5
				// // ,前面一截就是1234,后面一截就是6789

				if (back <= getPageCount()) {
					for (int i = 0; i < 4; i++)
						strb.append("<a href="
								+ goToPageURI(getPageIndex() + i + 1) + ">["
								+ (getPageIndex() + (i + 1) + 1) + "]</a>");
				} else {
					for (int i = 0; i < getPageCount() - getPageIndex() - 1; i++)
						strb.append("<a href="
								+ goToPageURI(getPageIndex() + i + 1) + ">["
								+ (getPageIndex() + (i + 1) + 1) + "]</a>");
				}
			}

			out.println(strb.toString());
			nextPage(out);
			lastPage(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	/**
	 * @param page
	 *            :跳转到的页码
	 */
	protected String goToPageURI(int page) {
		String requestPath = ((HttpServletRequest) pageContext.getRequest())
				.getRequestURI();
		String rp = null;

		String tid = (String) ((HttpServletRequest) pageContext.getRequest())
				.getParameter("typeid");

		if (null != tid && !"".equals(tid)) {
			Integer typeid = Integer.parseInt(tid);

			if (null == action) {
				rp = requestPath
						+ (requestPath.lastIndexOf("?") == -1 ? "?" : "&")
						+ "pageIndex=" + page + "&pageSize=" + getPageSize()
						+ "&recordCount=" + getRecordCount() + "&typeid="
						+ typeid + "&typeid2=" + typeid;
			} else {
				rp = requestPath + action
						+ (action.lastIndexOf("?") == -1 ? "?" : "&")
						+ "pageIndex=" + page + "&pageSize=" + getPageSize()
						+ "&recordCount=" + getRecordCount() + "&typeid="
						+ typeid + "&typeid2=" + typeid;
			}
		} else {
			if (null == action) {
				rp = requestPath
						+ (requestPath.lastIndexOf("?") == -1 ? "?" : "&")
						+ "pageIndex=" + page + "&pageSize=" + getPageSize()
						+ "&recordCount=" + getRecordCount();
			} else {
				rp = requestPath + action
						+ (action.lastIndexOf("?") == -1 ? "?" : "&")
						+ "pageIndex=" + page + "&pageSize=" + getPageSize()
						+ "&recordCount=" + getRecordCount();
			}
		}

		return rp;
	}

	private void getColor() {
		if (indexColor.equals(""))
			indexColor = "red";
		if (countColor.equals(""))
			countColor = "blue";
	}

	public String getCountColor() {
		return countColor;
	}

	public void setCountColor(String countColor) {
		this.countColor = countColor;
	}

	public String getIndexColor() {
		return indexColor;
	}

	public void setIndexColor(String indexColor) {
		this.indexColor = indexColor;
	}

	public int getPageSize() {
		String ps = this.pageContext.getRequest().getParameter("pageSize");
		if (null == ps || "".equals(ps)) {
			pageSize = 20;
		} else {
			pageSize = Integer.parseInt(ps);
		}
		return pageSize;
	}

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

	private int recordCount;// 总记录个数
	private List<?> list;

	public List<?> getList() {

		List<?> l = (List<?>) this.pageContext.getRequest().getAttribute(
				"listPage");

		if (null == l || l.size() == 0) {
			return new ArrayList();
		} else {
			return l;
		}

	}

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

	public int getRecordCount() {
		return getList().size();
	}

	public void setRecordCount(int recordCount) {
		this.recordCount = recordCount;
	}

}

 

 

 

================================我是分割线============================

 

TLD标签定义代码:

<tag>
		<name>PageForCMSTag2</name>
		<tag-class>g.cms.web.tag.PageForCMSTag2</tag-class>
		<body-content>jsp</body-content>
		<attribute>
			<name>pageIndex</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
			<type>int</type>
			<description>当前的页数</description>
		</attribute>
		<attribute>
			<name>pageCount</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
			<description>总页数</description>
		</attribute>
		<attribute>
			<name>pageSize</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<description>URL</description>
		</attribute>
		<attribute>
			<name>action</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
			<description>url自定义参数</description>
		</attribute>
		<attribute>
			<name>indexColor</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
			<description>当前所在页的参数</description>
		</attribute>
		<attribute>
			<name>countColor</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
			<description>总页数的参数</description>
		</attribute>
	</tag>

 

 

===============================我是分割线============================

 

效果图如下:

 

PAGE

 

 PAGE

 

 

PAGE

 

 

 

  • 大小: 60.7 KB
  • 大小: 74.7 KB
  • 大小: 67.8 KB
  • 大小: 56.2 KB
分享到:
评论
1 楼 luo476979657 2010-01-26  
您好!我想问问,我在页面上如何传递参数到自定义标签里去呢??

相关推荐

Global site tag (gtag.js) - Google Analytics