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(); }