一.在工具类中封装一个获取路径的方法,用于获取jsp页面的请求路径及相关参数
1 public static String getPath(HttpServletRequest request) { 2 String uri = request.getRequestURI(); 3 //获取请求路径/bookstore02/BookServlet 4 5 String queryString = request.getQueryString(); 6 //获取请求参数method=getPage&pageNumber=4 7 8 9 String path=uri+"?"+queryString; 10 //拼接字符串 11 12 //截取字符串 13 if(path.contains("&pageNumber")){ 14 path = path.substring(0, path.indexOf("&pageNumber")); 15 } 16 return path; 17 } 18
二.分页的具体实现
1.创建分页实体类(bean)
1 package com.neuedu.bean; 2 3 import java.util.List; 4 5 public class Page<T> { 6 7 private List<T> list; //当前页列表数据 8 private int pageNumber; //当前页 9 private int totalRecord; //总记录数 10 private int pageSize; //每页显示几条数据 11 12 // private int totalPage; //总页数(需要计算得到) 13 // private int index; //当前页的索引(需要计算得到) 14 15 private String path; 16 17 18 public Page() { 19 super(); 20 } 21 22 public List<T> getList() { 23 return list; 24 } 25 26 public int getPageNumber() { 27 if(pageNumber<1){ 28 pageNumber=1; 29 } 30 if(pageNumber>getTotalPage()){ 31 pageNumber=getTotalPage(); 32 } 33 return pageNumber; 34 } 35 36 public int getTotalPage() { 37 38 if(totalRecord%pageSize==0){ 39 return totalRecord/pageSize; 40 }else{ 41 return (totalRecord/pageSize)+1; 42 } 43 44 } 45 46 public int getTotalRecord() { 47 return totalRecord; 48 } 49 50 public int getPageSize() { 51 return pageSize; 52 } 53 54 public int getIndex() { //分页查询,在数据访问层调用getIndex方法获得索引值 55 /** 56 * pageNumber pageSize Index 57 * 1 3 0 58 * 2 3 3 59 * 3 3 6 60 * 61 * index=(pageNumber-1)*pageSize 62 */ 63 64 return (getPageNumber()-1)*getPageSize(); 65 66 } 67 68 public void setList(List<T> list) { 69 this.list = list; 70 } 71 72 public void setPageNumber(int pageNumber) { 73 this.pageNumber = pageNumber; 74 } 75 76 public void setTotalRecord(int totalRecord) { 77 this.totalRecord = totalRecord; 78 } 79 80 public void setPageSize(int pageSize) { 81 this.pageSize = pageSize; 82 } 83 84 @Override 85 public String toString() { 86 return "Page [list=" + list + ", pageNumber=" + pageNumber 87 + ", totalRecord=" + totalRecord 88 + ", pageSize=" + pageSize + "]"; 89 } 90 91 public Page(int pageNumber, int totalRecord, int pageSize) { 92 super(); 93 this.pageNumber = pageNumber; 94 this.totalRecord = totalRecord; 95 this.pageSize = pageSize; 96 } 97 98 public String getPath() { 99 return path; 100 } 101 102 public void setPath(String path) { 103 this.path = path; 104 } 105 106 }
2.创建Dao层
①创建BookDao接口
1 package com.neuedu.dao; 2 3 import java.util.List; 4 import com.neuedu.bean.Book; 5 6 public interface BookDao { 7 8 public int getTotalRecord(); //获取数据库总记录数 9 public List<Book> getBookList(int index, int pageSize); //得到请求页的数据列表 10 11 }
②创建BookDao接口的实现类BookDaoImpl
1 package com.neuedu.dao.impl; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.util.List; 7 import com.neuedu.utils.JDBCUtils; 8 import java.sql.Connection; 9 import java.sql.PreparedStatement; 10 import java.sql.ResultSet; 11 import java.util.ArrayList; 12 import com.neuedu.bean.Book; 13 import com.neuedu.dao.BookDao; 14 15 public class BookDaoImpl implements BookDao{ 16 17 private Connection conn=null; 18 private PreparedStatement ps=null; 19 private ResultSet rs=null; 20 21 22 //查询数据库中总记录数 23 public int getTotalRecord() { 24 ResultSet rs =null; 25 int totalRecord=0; 26 try { 27 conn= JDBCUtils.getConnection(); 28 String sql="select count(*) totalRecord from book"; 29 ps=conn.prepareStatement(sql); 30 rs = ps.executeQuery(); 31 32 if(rs.next()){ 33 totalRecord = rs.getInt("totalRecord"); 34 } 35 36 } catch (Exception e) { 37 e.printStackTrace(); 38 }finally{ 39 JDBCUtils.close(conn, ps, rs); 40 } 41 42 return totalRecord; 43 44 } 45 46 //访问数据库,通过limit (当前页的索引,每页显示几条数据) 实现分页查询,得到请求页的数据列表 47 public List<Book> getBookList(int index, int pageSize) { 48 ResultSet rs =null; 49 int totalRecord=0; 50 List<Book> list=new ArrayList<Book>(); 51 try { 52 conn= JDBCUtils.getConnection(); 53 String sql="select * from book limit ?,?"; 54 ps=conn.prepareStatement(sql); 55 ps.setInt(1, index); 56 ps.setInt(2, pageSize); 57 rs = ps.executeQuery(); 58 while(rs.next()){ 59 int id = rs.getInt("id"); 60 String bookName = rs.getString("book_name"); 61 String author = rs.getString("author"); 62 int price = rs.getInt("price"); 63 int sales = rs.getInt("sales"); 64 int stock = rs.getInt("stock"); 65 String imgPath = rs.getString("img_path"); 66 67 list.add(new Book(id, bookName, author, price, sales, stock, imgPath)); 68 } 69 70 } catch (Exception e) { 71 e.printStackTrace(); 72 }finally{ 73 JDBCUtils.close(conn, ps, rs); 74 } 75 return list; 76 } 77 }
3.创建service层
获取当前页的数据列表
1 package com.neuedu.service; 2 3 import java.util.List; 4 import com.neuedu.bean.Book; 5 import com.neuedu.bean.Page; 6 import com.neuedu.dao.impl.BookDaoImpl; 7 8 public class BookService { 9 10 private BookDaoImpl dao =new BookDaoImpl(); 11 12 13 public Page<Book> getPage(int pageSize,String pageNumber){ 14 int pageNum=1; 15 Page<Book> page=null; 16 try{ 17 pageNum=Integer.parseInt(pageNumber); 18 }catch(Exception e) { 19 } 20 //1.获取总记录数 21 int totalRecord=dao.getTotalRecord(); 22 //2.封装page对象 23 page=new Page<>(pageNum,totalRecord,pageSize); 24 //3.查询当前页对应的数据列表并设置到page对象中 25 List<Book> list=dao.getBookList(page.getIndex(),pageSize); 26 page.setList(list); 27 28 return page; 29 } 30 31 32 }
4.创建servlet层
1 package com.neuedu.web; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import com.neuedu.bean.Book; 12 import com.neuedu.bean.Page; 13 import com.neuedu.service.BookService; 14 import com.neuedu.utils.WEBUtils; 15 16 17 public class BookServlet extends BaseServlet { 18 private static final long serialVersionUID = 1L; 19 20 private BookService service=new BookService(); 21 22 public void getPage(HttpServletRequest request, HttpServletResponse response){ 23 24 String path=WEBUtils.getPath(request); 25 String pageNumber = request.getParameter("pageNumber"); 26 Page<Book> page = service.getPage(pageSize, pageNumber); 27 page.setPath(path); 28 29 request.setAttribute("page", page); 30 try { 31 request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response); 32 } catch (Exception e) { 33 e.printStackTrace(); 34 } 35 } 36 } 37
三.前台页面的实现
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <div id="page_nav"> 6 <a href="${page.path}&pageNumber=1">首页</a> 7 <c:if test="${page.pageNumber>1}"> 8 <a href="${page.path}&pageNumber=${page.pageNumber-1}">上一页</a> 9 </c:if> 10 11 <c:choose> 12 <c:when test="${page.totalPage<=5}"> 13 <c:set var="begin" value="1"></c:set> 14 <c:set var="end" value="${page.totalPage}"></c:set> 15 </c:when> 16 <c:when test="${page.pageNumber<=3}"> 17 <c:set var="begin" value="1"></c:set> 18 <c:set var="end" value="5"></c:set> 19 </c:when> 20 <c:when test="${page.pageNumber>3}"> 21 <c:set var="begin" value="${page.pageNumber-2 }"></c:set> 22 <c:set var="end" value="${page.pageNumber+2 }"></c:set> 23 <c:if test="${page.pageNumber+2>=page.totalPage}"> 24 <c:set var="end" value="${page.totalPage}"></c:set> 25 <c:set var="begin" value="${page.totalPage-4}"></c:set> 26 </c:if> 27 </c:when> 28 </c:choose> 29 30 <c:forEach begin="${begin}" end="${end}" var="index"> 31 <c:if test="${page.pageNumber==index}"> 32 <a href="${page.path}&pageNumber=${page.pageNumber}">【${index}】</a> 33 </c:if> 34 <c:if test="${page.pageNumber!=index}"> 35 <a href="${page.path}&pageNumber=${index}">${index}</a> 36 </c:if> 37 </c:forEach> 38 39 <c:if test="${page.pageNumber<page.totalPage}"> 40 <a href="${page.path}&pageNumber=${page.pageNumber+1}">下一页</a> 41 </c:if> 42 43 <a href="${page.path}&pageNumber=${page.totalPage}">末页</a> 44 共${page.totalPage}页,${page.totalRecord}条记录到第<input value="${page.pageNumber}" name = "pn" id ="pn_input"/>页 45 <input id="btn" type="button" value="确定"/> 46 47 <script type="text/javascript"> 48 $(function(){ 49 $("#btn").click(function(){ 50 var pageNum=$("#pn_input").val(); 51 window.location="${page.path}&pageNumber="+pageNum; 52 }); 53 }); 54 </script> 55 56 </div>