struts+spring+hibernate+自定义标签的 分页方案

首先编写一个分页用的类

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.xfaccp.hibernate.dao.CustomersDAO;

public class PagTag extends BodyTagSupport {

	private HttpServletRequest request;

	private int pageSize; // 每页显示的记录数

	private String tableName = null; // 表名

	private int tableSize = 0; // 表中信息的总数量

	private int pageCount = 0; // 共有几页

	private int pages = 1; // 当前页数

	private int startNum; // 起始记录数

	private CustomersDAO cDAO = null;

	private List result = null;

	private HttpSession session = null;



	private String operate;

	
	// 设置分页基本信息
	public int baseSetting() {

		// 计算总页数
		if (tableSize % pageSize != 0) {
			this.pageCount = (tableSize / pageSize) + 1;
		} else {
			this.pageCount = tableSize / pageSize;
		}
		// 设置总共有多少页
		pageContext.setAttribute("pageCount", new Integer(pageCount));

		if (request.getParameter("pages") != null) {
			this.pages = Integer.parseInt(request.getParameter("pages"));
			System.out.println("pages=" + pages);
		}
		if (pages < 1) {
			pages = 1;
		}
		if (pages > pageCount) {
			pages = pageCount;
		}
		// 设置当为第几页
		pageContext.setAttribute("pages", new Integer(pages));

		startNum = pageSize * (pages - 1);
		if (startNum < 0) {
			startNum = 1;
		}
		return startNum;

	}

	public int doStartTag() {
		session = request.getSession();
		cDAO = new CustomersDAO();
		
		if (request.getParameter("operate") != null)
			this.operate = request.getParameter("operate");
		System.out.println("operate"+operate);
		try {
			if (operate == null) {
				System.out.println("accp");
				tableSize = cDAO.getTableSize(); // 设置表中信息的总数量

				int startNm = this.baseSetting();

				System.out.println("startNum" + startNum);
				System.out.println("pagesize" + pageSize);
				result = cDAO.getTableList(startNm, pageSize);
				
			} else {
				tableSize = cDAO.searchTablePartSize();
				int startNum=this.baseSetting();
				result=cDAO.searchTableList(startNum, pageSize);
			}
			session.setAttribute("result", result);

		} catch (Exception sl) {
			sl.printStackTrace();

		}
		return EVAL_BODY_INCLUDE;
	}
	
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getOperate() {
		return operate;
	}

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

	public int getPageSize() {
		return pageSize;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}


}


然后编写一个tld 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
	"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>
	<tlib-version>1.0</tlib-version>
	<jsp-version>1.1</jsp-version>
	<short-name>show</short-name>
	<uri>http://com.yandong.page</uri>
	<tag>
		<name>show</name>
		<tag-class>com.yandong.common.tag.PagTag</tag-class>
		<body-content>jsp</body-content>
		<attribute>
			<name>request</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>pageSize</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>tableName</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>

</taglib>

经过这样的配置就ok了,业务逻辑我就不写了

你可能感兴趣的:(spring,Hibernate,jsp,struts,servlet)