在工具类中封装一个获取路径的方法,用于获取jsp页面的请求路径及相关参数
1 public static String getPath(HttpServletRequest request) {2String uri = request.getRequestURI();3//获取请求路径/bookstore02/BookServlet45String queryString = request.g...
·
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)
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
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>
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)
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
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>
更多推荐
已为社区贡献1条内容
所有评论(0)