SSH分页+Mysql

结构+效果图


       


Page.java :

public class Page {

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

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

	/** 每页的数量 */
	private int everyPage;

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

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

	/** 起始点 */
	private int beginIndex;

	/** 总记录数 */
	private int totalCount;

	/**
	 * @return totalCount
	 */
	public int getTotalCount() {
		return totalCount;
	}

	/**
	 * @param totalCount
	 *            要设置的 totalCount
	 */
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	/** The default constructor */
	public Page() {

	}

	/**
	 * construct the page by everyPage
	 * 
	 * @param everyPage
	 * */
	public Page(int everyPage) {
		this.everyPage = everyPage;
	}

	/** The whole constructor */
	public Page(boolean hasPrePage, boolean hasNextPage, int everyPage,
			int totalPage, int currentPage, int beginIndex, int totalCount) {
		this.hasPrePage = hasPrePage;
		this.hasNextPage = hasNextPage;
		this.everyPage = everyPage;
		this.totalPage = totalPage;
		this.currentPage = currentPage;
		this.beginIndex = beginIndex;
		this.totalCount = totalCount;
	}

	/**
	 * @return Returns the beginIndex.
	 */
	public int getBeginIndex() {
		return beginIndex;
	}

	/**
	 * @param beginIndex
	 *            The beginIndex to set.
	 */
	public void setBeginIndex(int beginIndex) {
		this.beginIndex = beginIndex;
	}

	/**
	 * @return Returns the currentPage.
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * @param currentPage
	 *            The currentPage to set.
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	/**
	 * @return Returns the everyPage.
	 */
	public int getEveryPage() {
		return everyPage;
	}

	/**
	 * @param everyPage
	 *            The everyPage to set.
	 */
	public void setEveryPage(int everyPage) {
		this.everyPage = everyPage;
	}

	/**
	 * @return Returns the hasNextPage.
	 */
	public boolean getHasNextPage() {
		return hasNextPage;
	}

	/**
	 * @param hasNextPage
	 *            The hasNextPage to set.
	 */
	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	/**
	 * @return Returns the hasPrePage.
	 */
	public boolean getHasPrePage() {
		return hasPrePage;
	}

	/**
	 * @param hasPrePage
	 *            The hasPrePage to set.
	 */
	public void setHasPrePage(boolean hasPrePage) {
		this.hasPrePage = hasPrePage;
	}

	/**
	 * @return Returns the totalPage.
	 * 
	 */
	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * @param totalPage
	 *            The totalPage to set.
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	@Override
	public String toString() {
		return "Page [hasPrePage(是否有上一页)=" + hasPrePage
				+ ", hasNextPage(是否有下一页)=" + hasNextPage
				+ ", everyPage(每页的数量)=" + everyPage + ", totalPage(总页数)="
				+ totalPage + ", currentPage(当前页)=" + currentPage
				+ ", beginIndex(起始点)=" + beginIndex + ", totalCount(总记录数)="
				+ totalCount + "]";
	}

}

PageUtil.java

public class PageUtil {
	/**
	 * Use the origin page to create a new page
	 * 
	 * @param page
	 * @param totalRecords
	 * @return
	 */
	public static Page createPage(Page page, int totalRecords) {
		return createPage(page.getEveryPage(), page.getCurrentPage(),
				totalRecords);
	}

	/**
	 * the basic page utils not including exception handler
	 * 
	 * @param everyPage
	 * @param currentPage
	 * @param totalRecords
	 * @return page
	 */
	public static Page createPage(int everyPage, int currentPage, int totalRecords) {
		everyPage = getEveryPage(everyPage);
		currentPage = getCurrentPage(currentPage);
		int beginIndex = getBeginIndex(everyPage, currentPage);
		int totalPage = getTotalPage(everyPage, totalRecords);
		boolean hasNextPage = hasNextPage(currentPage, totalPage);
		boolean hasPrePage = hasPrePage(currentPage);

		return new Page(hasPrePage, hasNextPage, everyPage, totalPage,
				currentPage, beginIndex, totalRecords);
	}

	private static int getEveryPage(int everyPage) {
		return everyPage == 0 ? 10 : everyPage;
	}

	private static int getCurrentPage(int currentPage) {
		return currentPage == 0 ? 1 : currentPage;
	}

	private static int getBeginIndex(int everyPage, int currentPage) {
		return (currentPage - 1) * everyPage;
	}

	private static int getTotalPage(int everyPage, int totalRecords) {
		int totalPage = 0;

		if (totalRecords % everyPage == 0)
			totalPage = totalRecords / everyPage;
		else
			totalPage = totalRecords / everyPage + 1;

		return totalPage;
	}

	private static boolean hasPrePage(int currentPage) {
		return currentPage == 1 ? false : true;
	}

	private static boolean hasNextPage(int currentPage, int totalPage) {
		return currentPage == totalPage || totalPage == 0 ? false : true;
	}

}

PageAction

public class PageAction extends ActionSupport {

	private StudentDAO studentDao = null;
	private List<Student> list = null;
	private Integer count = null;
	private Page page = null;
	private Integer everyPage = 3;// 默认每页显示3条数据
	private Integer currentPage = 1;// 默认显示第一页

	public Integer getEveryPage() {
		return everyPage;
	}

	public void setEveryPage(Integer everyPage) {
		this.everyPage = everyPage;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public void setStudentDao(StudentDAO studentDao) {
		this.studentDao = studentDao;
	}

	public List<Student> getList() {
		return list;
	}

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

	public Integer getCount() {
		return count;
	}

	public void setCount(Integer count) {
		this.count = count;
	}

	public Page getPage() {
		return page;
	}

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

	public String execute() throws Exception {
		page = PageUtil.createPage(everyPage, currentPage,
				studentDao.getProductCount());
		System.out.println(page);
		list = studentDao.getProductByPage(page);// 得到分页数据
		return "success";
	}

}

DaoImpl

public List getProductByPage(final Page page) {
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery("from Student");
				query.setFirstResult(page.getBeginIndex());
				query.setMaxResults(page.getEveryPage());
				return query.list();
			}
		});
	}

	public int getProductCount() {
		List list = this.getHibernateTemplate().find("from Student");
		return list.size();
	}

jsp

<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>SSH分页案例</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <table align="center" border="1" width="100%">
	    <tr>
		    <td align="center">ID</td>
		    <td align="center">姓名</td>
		    <td align="center">性别</td>
		    <td align="center">年龄</td>
		    <td align="center">学校</td>
	    </tr>
	    <c:forEach items="${list }" var="student">
	    <tr>
	    	<td align="center">${student.id }</td>
	    	<td align="center">${student.name }</td>
	    	<td align="center">${student.sex }</td>
	    	<td align="center">${student.age }</td>
	    	<td align="center">${student.school }</td>
	    </tr>
	    </c:forEach>
    </table><br>
    <div align="center">
    	每页显示:<select οnchange="window.location.href=this.options[selectedIndex].value">
	    	<option value="pageAction?everyPage=3¤tPage=1">3</option>
	    	<option value="pageAction?everyPage=5¤tPage=1">5</option>
	    	<option value="pageAction?everyPage=8¤tPage=1">8</option>
    	</select>  条
	 	当前页数:[${page.currentPage }/${page.totalPage }]
		<c:if test="${page.currentPage>1 }">
			<a href="pageAction?currentPage=1">首页</a>  
			<a href="pageAction?currentPage=${page.currentPage-1 }">上一页</a>
		</c:if> 
		<c:if test="${page.currentPage<page.totalPage }">
			<a href="pageAction?currentPage=${page.currentPage+1 }">下一页</a>
			<a href="pageAction?currentPage=${page.totalPage }">末页</a>
		</c:if>
		总记录数:${page.totalCount }  条
	</div>
  </body>
</html>


详细源码下载地址:http://download.csdn.net/detail/u014676619/9236741



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值