數據庫表格:
customer
分頁的實現:
從數據庫查詢--總記錄:totalRecords
自定義--每頁顯示多少條記錄: pageSize
通過總記錄數和頁容量計算出來--總頁數: totalPages ----(totalRecords%pageSize==0? totalRecords/pageSize : totalRecords/pageSize+1)
頁面傳遞--當前頁碼: currentPage
從數據庫查詢--每頁數據:List
通過當前頁數和頁容量計算出來--每頁查詢開始的索引 startIndex startIndex = (currentPage-1)*pageSize
bean
public class Page { private long totalRecords;//總記錄數 從數據庫查詢 private long pageSize=5;//每頁顯示多少條 private long totalPages;//總頁數 計算出來 private long currentPage;//當前頁碼 private List list; //每頁存放的數據 從數據庫查詢 private long startIndex;//每頁開始查詢的索引 計算出來 public Page(long currentPage,long totalRecords){ this.currentPage = currentPage; this.totalRecords = totalRecords; this.totalPages = (totalRecords%pageSize==0? totalRecords/pageSize : totalRecords/pageSize+1); this.startIndex = (currentPage-1)*pageSize; } public long getTotalRecords() { return totalRecords; } public void setTotalRecords(long totalRecords) { this.totalRecords = totalRecords; } public long getPageSize() { return pageSize; } public void setPageSize(long pageSize) { this.pageSize = pageSize; } public long getTotalPages() { return totalPages; } public void setTotalPages(long totalPages) { this.totalPages = totalPages; } public long getCurrentPage() { return currentPage; } public void setCurrentPage(long currentPage) { this.currentPage = currentPage; } public List getList() { return list; } public void setList(List list) { this.list = list; } public long getStartIndex() { return startIndex; } public void setStartIndex(long startIndex) { this.startIndex = startIndex; } }
Dao.Impl
public class CustomerDaoImpl implements CustomerDao{ QueryRunner qr = new QueryRunner(C3p0Util.getDataSource()); //顯示列表 public List<Customer> findCustomers() { String sql = "select * from customer"; List<Customer> clist = new ArrayList<Customer>(); try { clist = qr.query(sql, new BeanListHandler<Customer>(Customer.class)); } catch (SQLException e) { e.printStackTrace(); } return clist; } //查詢每頁記錄數據 public List<Customer> findPageCustomers(long startIndex, long pageSize) { String sql = "select * from customer limit ?,?"; Object[] params = {startIndex,pageSize}; List<Customer> clist = new ArrayList<Customer>(); try { clist = qr.query(sql, new BeanListHandler<Customer>(Customer.class),params); } catch (SQLException e) { e.printStackTrace(); } return clist; } //查詢總記錄數 public long findCustomersCount() { String sql = "select count(1) from customer"; long i= 0; try { i = (Long)qr.query(sql, new ScalarHandler(1)); } catch (SQLException e) { e.printStackTrace(); } return i; } }
Service.Impl
public class CustomerServiceImpl implements CustomerService{ private CustomerDao dao = new CustomerDaoImpl(); //顯示列表 public List<Customer> findCustomers() { return dao.findCustomers(); } //與頁碼相對應的每個列表 public Page findPage(long currentPage) { long totalRecords = dao.findCustomersCount();//總記錄數 Page page = new Page(currentPage, totalRecords); List<Customer> clist = dao.findPageCustomers(page.getStartIndex(), page.getPageSize()); page.setList(clist); return page; } }
Servlte(在web.xml里設置默認頁面為servlte頁面)
/** * servlet頁面 * @author Administrator *下午1:56:49 */ public class CustomerAction extends HttpServlet { private CustomerService service = new CustomerServiceImpl(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String currentPageStr = request.getParameter("pagenum"); //從網頁上獲取跳轉的頁數 int currentPage = 1; //默認顯示第一頁 if(currentPageStr!=null){ currentPage = Integer.parseInt(currentPageStr); } Page page = service.findPage(currentPage);//顯示與頁碼相對應的列表 request.setAttribute("page", page); request.getRequestDispatcher("WEB-INF/jsp/clist.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
jsp(由servlet處理后轉發至jsp頁面)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>客戶列表</title> <c:set var="ctp" value="${pageContext.request.contextPath }"></c:set> <script type="text/javascript" src="${ctp }/js/jquery-1.8.3.js"></script> </head> <body> <h2>客戶列表</h2> <table border="1" width="80%"> <tr> <th>序號</th> <th>姓名</th> <th>性別</th> <th>生日</th> <th>電話</th> <th>郵箱</th> <th>愛好</th> <th>類型</th> <th>描述</th> </tr> <c:forEach items="${page.list }" var="ct" varStatus="cs"> <!--使用迭代標簽庫 --> <tr style="background-color: ${cs.count%2==0? '#E0FFFF':'#EEE685'}"> <td>${cs.count }</td> <td>${ct.name }</td> <td>${ct.gender==1? '男':'女' }</td> <td>${ct.birthday }</td> <td>${ct.phone }</td> <td>${ct.email }</td> <td>${ct.hobby }</td> <td>${ct.type }</td> <td>${ct.description }</td> </tr> </c:forEach> </table> <p> 總記錄數:${page.totalRecords }條 當前第${page.currentPage }頁/共${page.totalPages }頁 <a href="${ctp }/customerAction?pagenum=1">首頁</a> <a href="${ctp }/customerAction?pagenum=${page.currentPage-1<1? 1 : page.currentPage-1}">上一頁</a> <a href="${ctp }/customerAction?pagenum=${page.currentPage+1<page.totalPages? page.currentPage+1 : page.totalPages}">下一頁</a> <a href="${ctp }/customerAction?pagenum=${page.totalPages}">尾頁</a> <input id="num" placeholder="請輸入整數"/><input type="button" id="btn" value="跳轉"/> <input id="pages" value="${page.totalPages }" type="hidden"> </p> <!--使用jQuery簡單判定跳轉的頁數是否合法--> <script type="text/javascript"> $(function(){ $("#btn").click(function(){ var pageNum = Number($("#num").val()); var pages = Number($("#pages").val()); if(isNaN(pageNum)){ alert("請輸入數字!"); return; } if(pageNum > pages){ alert("輸入頁碼錯誤!"); return; } location.href = "${ctp}/customerAction?pagenum="+pageNum; }); }); </script> </body> </html>
效果圖: