`
pengjianbo1
  • 浏览: 229954 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

自定义标签分页

    博客分类:
  • J2EE
阅读更多

首先创建PageBean

 

package com.pjb.sys.tag;

public class PageBean {

	private int totalResults; // 总记录数

	public static int pageSize = 5; // 每页显示的记录数

	private int currentPage = 1; // 当前页号

	public PageBean(int totalResults, int pageSize) {
		super();
		this.totalResults = totalResults;
		PageBean.pageSize = pageSize;
	}

	public PageBean() {

	}

	/**
	 * 
	 * @return 得到总记录数
	 */
	public int getTotalResults() {
		return totalResults;
	}

	public void setTotalResults(int totalResults) {
		this.totalResults = totalResults;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		if (this.currentPage < 1) {
			this.currentPage = 1;
		}
		if (this.currentPage > this.totalResults) {
			this.currentPage = this.totalResults;
		}
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

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

	public int getTotalPageSize() {
		return (int) Math.ceil(this.totalResults / (double) PageBean.pageSize);
	}

	/**
	 * 是否有下一页
	 * 
	 * @return
	 */
	public boolean IsNextPage() {
		if (this.currentPage < this.getTotalPageSize()) {
			return true;
		}
		return false;
	}

	/**
	 * 是否有上一页
	 */
	public boolean IsUpPage() {
		if (this.currentPage > 1) {
			return true;
		}
		return false;
	}

	/**
	 * 从哪一条开始取
	 */

	public int getStartResult() {
		return (this.currentPage - 1) * PageBean.pageSize;
	}

}

 

标签PageTag类

 

package com.pjb.sys.tag;

import java.io.IOException;

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

@SuppressWarnings("serial")
public class PageTag extends TagSupport {

	// 请求的action
	private String action;

	public String getAction() {
		return action;
	}

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

	@Override
	public int doEndTag() throws JspException {

		JspWriter out = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		PageBean pageBean = (PageBean) request.getAttribute("pageBean");

		try {
			out.println("<table>");
			out.println("<tr>");
			out.println("<td>");
			out.println("第" + pageBean.getCurrentPage() + "页");
			out.println("共" + pageBean.getTotalPageSize() + "页" + "共" + pageBean.getTotalResults() + "记录");
			out.println("<a href=\"" + request.getContextPath() + action + "1\">首页</a>");
			out.println("<a href=\"" + request.getContextPath() + action + pageBean.getTotalPageSize() + "\">尾页</a>");

			if (pageBean.IsUpPage()) {

				out.println("<a href=\"" + request.getContextPath() + action + (pageBean.getCurrentPage() - 1)
						+ "\">上页</a>");

			} else {

				out.println("上页");

			}
			if (pageBean.IsNextPage()) {

				out.println("<a href=\"" + request.getContextPath() + action + (pageBean.getCurrentPage() + 1)
						+ "\">下页</a>");

			} else {

				out.println("下页");

			}

			out.println("<select onchange=\"self.location='" + request.getContextPath() + action
					+ "'+this.options[this.selectedIndex].value\">");
			for (int i = 1; i <= pageBean.getTotalPageSize(); i++) {

				if (pageBean.getCurrentPage() == i) {
					out.println("<option value=" + i + " selected>" + i + "</option>");
				} else {
					out.println("<option value=" + i + ">" + i + "</option>");
				}
			}
			out.println("</select>");
			out.println("</td>");
			out.println("</tr>");
			out.println("</table>");

		} catch (IOException e) {

			e.printStackTrace();
		}

		return EVAL_PAGE;
	}

}

 

 

创建mytag.tld文件

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "web-jsptaglibrary_1_2.dtd" >
<taglib>
	<tlib-version>1.0</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>my</short-name>
	<tag>
		<name>cutPage</name>
		<tag-class>com.pjb.sys.tag.PageTag</tag-class>
		<attribute>
			<name>action</name>
			<required>true</required>
			<description>请求action 如:showBookPage.do?page</description>
		</attribute>
	</tag>
</taglib>

 

在页面中使用时,需导入标签定义

 

<%@ taglib prefix="my" uri="/WEB-INF/mytag.tld"%>

 

引用标签名

 

<my:cutPage action="/readerManagerAction.do?method=getReaderList&page="></my:cutPage>

 

 action 代码

 

/**
	 * 查询列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward getReaderList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			ReaderService rs = (ReaderService) this.getBean("readerService");
			PageBean pageBean = new PageBean();
			String pageStr = request.getParameter("page");
			if (pageStr != null) {
				pageBean.setCurrentPage(Integer.parseInt(pageStr));
			}
			List<ReaderBean> list = rs.getReaderList(pageBean);
			request.setAttribute("list", list);
			request.setAttribute("pageBean", pageBean);
			return mapping.findForward("showReaderList");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			request.setAttribute("errMsg", e.getMessage());
		}
		return mapping.findForward("error");
	}

 

ibatis分页取记录DAO

 

 

public List<ReaderBean> getReaderList(PageBean pageBean) throws FrameworkDaoException {
		try {
			List readerList = this.getSqlMapClientTemplate().queryForList("getReaderList", null);
			pageBean.setTotalResults(readerList.size());
			return this.getSqlMapClientTemplate().queryForList("getReaderList", null, pageBean.getStartResult(), pageBean.getPageSize());
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new FrameworkDaoException(e);
		}
	}

  

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics