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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章