jsp
<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>全部圖書</title> <link href="<%=basePath%>resources/css/pagination.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="<%=basePath%>resources/js/jquery-1.11.3.min.js"></script> <script type="text/javascript" src="<%=basePath%>resources/js/jquery.pagination.js"></script> <script type="text/javascript"> // 點擊分頁按鈕以后觸發的動作 function handlePaginationClick(new_page_index, pagination_container) { $("#stuForm").attr("action", "<%=basePath %>booklist_jsp?pageNo="+ (new_page_index+1)); $("#stuForm").submit(); return false; } $(function(){ $("#News-Pagination").pagination(${pager.totalRecord}, { items_per_page:${pager.pageSize}, // 每頁顯示多少條記錄 current_page:${pager.pageNo} - 1, // 當前顯示第幾頁數據 num_display_entries:8, // 分頁插件中間顯示的按鈕數目 next_text:"下一頁", prev_text:"上一頁", num_edge_entries:2, // 連接分頁主體,顯示的條目數 callback:handlePaginationClick }); }); </script> </head> <body> <h3>全部圖書</h3> <!-- 回調函數調用控件 --> <form action="" id="stuForm" method="post"> </form> <table width="640" border="1"> <c:forEach items="${ pager.dataList}" var="book"> <tr> <td>${book.id}</td> <td>${book.name}</td> <td>${book.price}</td> <td>${book.author}</td> <td><a href="${pageContext.request.contextPath}/deleteBook?id=${book.id}">刪除</a></td> </tr> </c:forEach> </table> <br/> <div id="News-Pagination"></div> </body> </html>
引入jQuery要在pagination之前
pager實體類
package com.dr.domain; import java.util.List; /** * 定義一個分頁對象 * @author acer * */ public class Pager<T> { private int pageSize; // 每頁顯示多少條記錄 private int pageNo; //當前第幾頁數據 private int totalRecord; // 一共多少條記錄 private int totalPage; // 一共多少頁記錄 private List<T> dataList; //要顯示的數據 public Pager(){ } public Pager(int pageSize, int currentPage, int totalRecord, int totalPage, List<T> dataList) { super(); this.pageSize = pageSize; this.pageNo = currentPage; this.totalRecord = totalRecord; this.totalPage = totalPage; this.dataList = dataList; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getTotalRecord() { return totalRecord; } public void setTotalRecord(int totalRecord) { this.totalPage = this.totalRecord / this.pageSize; if(this.totalRecord % this.pageSize !=0){ this.totalPage = this.totalPage + 1; } this.totalRecord = totalRecord; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List<T> getDataList() { return dataList; } public void setDataList(List<T> dataList) { this.dataList = dataList; } }
controller
@RequestMapping("/booklist_jsp") public String ListBook( @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo, Model model) { Pager<Book> pager = new Pager<Book>(); pager.setPageNo(pageNo); pager.setPageSize(5); model.addAttribute("pager", bookService.find(pager)); return "listBooks_jsp"; }
servers
@Override public Pager<Book>find(Pager<Book> pager) { // TODO Auto-generated method stub List<Book> books = bookDao.findByPage("from Book", pager.getPageNo(), pager.getPageSize()); pager.setDataList(books); pager.setTotalRecord((int)bookDao.findCount(Book.class)); return pager; }
dao
@SuppressWarnings("unchecked") protected List<T> find(String hql) { return (List<T>)getSessionFactory().getCurrentSession() .createQuery(hql) .list(); }