IGeekShop案例6-商品的分页显示

迷南。 2024-04-17 14:08 171阅读 0赞

IGeekShop案例6-商品的分页显示

1 预期效果

在这里插入图片描述

2 pageBean类

  1. package www.test.vo;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class PageBean<T> {
  5. // 1 当前页
  6. private int currentPage;
  7. // 2 当前页显示的条数
  8. private int currentCount;
  9. // 3 总条数
  10. private int totalCount;
  11. // 4 总页数
  12. private int totalPage;
  13. // 5 每页显示的数据
  14. private List<T> productList = new ArrayList<T>();
  15. public int getCurrentPage() {
  16. return currentPage;
  17. }
  18. public void setCurrentPage(int currentPage) {
  19. this.currentPage = currentPage;
  20. }
  21. public int getCurrentCount() {
  22. return currentCount;
  23. }
  24. public void setCurrentCount(int currentCount) {
  25. this.currentCount = currentCount;
  26. }
  27. public int getTotalCount() {
  28. return totalCount;
  29. }
  30. public void setTotalCount(int totalCount) {
  31. this.totalCount = totalCount;
  32. }
  33. public int getTotalPage() {
  34. return totalPage;
  35. }
  36. public void setTotalPage(int totalPage) {
  37. this.totalPage = totalPage;
  38. }
  39. public List<T> getProductList() {
  40. return productList;
  41. }
  42. public void setProductList(List<T> productList) {
  43. this.productList = productList;
  44. }
  45. }

3 web层

  1. package www.test.web;
  2. import java.io.IOException;
  3. import java.sql.SQLException;
  4. import java.util.List;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import www.test.domain.Product;
  10. import www.test.service.ProductService;
  11. import www.test.vo.PageBean;
  12. public class ProductListServlet extends HttpServlet {
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  14. throws ServletException, IOException {
  15. ProductService service = new ProductService();
  16. //模拟当前是第一页
  17. String currentPageStr = request.getParameter("currentPage");
  18. //如果是直接访问productList的话,让currentPageStr=1;
  19. if(currentPageStr==null) currentPageStr="1";
  20. int currentPage = Integer.parseInt(currentPageStr);
  21. //认为每页显示12条
  22. int currentCount = 12;
  23. PageBean<Product> pageBean = null;
  24. try {
  25. pageBean = service.findPageBean(currentPage,currentCount);
  26. } catch (SQLException e) {
  27. e.printStackTrace();
  28. }
  29. request.setAttribute("pageBean", pageBean);
  30. request.getRequestDispatcher("/product_list.jsp").forward(request, response);
  31. }
  32. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  33. throws ServletException, IOException {
  34. doGet(request, response);
  35. }
  36. }

4 service层

  1. package www.test.service;
  2. import java.sql.SQLException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import www.test.dao.ProductDao;
  6. import www.test.domain.Product;
  7. import www.test.vo.PageBean;
  8. public class ProductService {
  9. public List<Product> findAllProduct() throws SQLException {
  10. ProductDao dao = new ProductDao();
  11. return dao.findAllProduct();
  12. }
  13. //分页操作
  14. public PageBean findPageBean(int currentPage,int currentCount) throws SQLException {
  15. ProductDao dao = new ProductDao();
  16. //目的:就是想办法封装一个PageBean 并返回
  17. PageBean pageBean = new PageBean();
  18. //1、当前页private int currentPage;
  19. pageBean.setCurrentPage(currentPage);
  20. //2、当前页显示的条数private int currentCount;
  21. pageBean.setCurrentCount(currentCount);
  22. //3、总条数private int totalCount;
  23. int totalCount = dao.getTotalCount();
  24. pageBean.setTotalCount(totalCount);
  25. //4、总页数private int totalPage;
  26. /*
  27. * 总条数 当前页显示的条数 总页数
  28. * 10 4 3
  29. * 11 4 3
  30. * 12 4 3
  31. * 13 4 4
  32. *
  33. * 公式:总页数=Math.ceil(总条数/当前显示的条数)
  34. *
  35. */
  36. int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
  37. pageBean.setTotalPage(totalPage);
  38. //5、每页显示的数据private List<T> productList = new ArrayList<T>();
  39. /*
  40. * 页数与limit起始索引的关系
  41. * 例如 每页显示4条
  42. * 页数 其实索引 每页显示条数
  43. * 1 0 4
  44. * 2 4 4
  45. * 3 8 4
  46. * 4 12 4
  47. *
  48. * 索引index = (当前页数-1)*每页显示的条数
  49. *
  50. */
  51. int index = (currentPage-1)*currentCount;
  52. List<Product> productList = dao.findProductListForPageBean(index,currentCount);
  53. pageBean.setProductList(productList);
  54. return pageBean;
  55. }
  56. }

5 dao层

  1. package www.test.dao;
  2. import java.sql.SQLException;
  3. import java.util.List;
  4. import org.apache.commons.dbutils.QueryRunner;
  5. import org.apache.commons.dbutils.handlers.BeanListHandler;
  6. import org.apache.commons.dbutils.handlers.ScalarHandler;
  7. import www.test.domain.Product;
  8. import www.test.utils.DataSourceUtils;
  9. public class ProductDao {
  10. public List<Product> findAllProduct() throws SQLException {
  11. return new QueryRunner(DataSourceUtils.getDataSource()).query("select * from product", new BeanListHandler<Product>(Product.class));
  12. }
  13. //获得全部的商品条数
  14. public int getTotalCount() throws SQLException {
  15. QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
  16. String sql = "select count(*) from product";
  17. Long query = (Long) runner.query(sql, new ScalarHandler());
  18. return query.intValue();
  19. }
  20. //获得分页的商品数据
  21. public List<Product> findProductListForPageBean(int index,int currentCount) throws SQLException {
  22. QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
  23. String sql = "select * from product limit ?,?";
  24. return runner.query(sql, new BeanListHandler<Product>(Product.class), index,currentCount);
  25. }
  26. }

6 product_list.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <title>会员登录</title>
  8. <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
  9. <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
  10. <script src="js/bootstrap.min.js" type="text/javascript"></script>
  11. <!-- 引入自定义css文件 style.css -->
  12. <link rel="stylesheet" href="css/style.css" type="text/css" />
  13. <style>
  14. body {
  15. margin-top: 20px;
  16. margin: 0 auto;
  17. width: 100%;
  18. }
  19. .carousel-inner .item img {
  20. width: 100%;
  21. height: 300px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <!-- 引入header.jsp -->
  27. <jsp:include page="/header.jsp"></jsp:include>
  28. <div class="row" style="width: 1210px; margin: 0 auto;">
  29. <div class="col-md-12">
  30. <ol class="breadcrumb">
  31. <li><a href="#">首页</a></li>
  32. </ol>
  33. </div>
  34. <c:forEach items="${pageBean.productList }" var="product">
  35. <div class="col-md-2" style="height:250px">
  36. <a href="product_info.htm">
  37. <img src="${pageContext.request.contextPath }/${product.pimage}" width="170" height="170" style="display: inline-block;">
  38. </a>
  39. <p>
  40. <a href="product_info.html" style='color: green'>${product.pname }</a>
  41. </p>
  42. <p>
  43. <font color="#FF0000">商城价:¥${product.shop_price }</font>
  44. </p>
  45. </div>
  46. </c:forEach>
  47. </div>
  48. <!--分页 -->
  49. <div style="width: 380px; margin: 0 auto; margin-top: 50px;">
  50. <ul class="pagination" style="text-align: center; margin-top: 10px;">
  51. <!-- 上一页 -->
  52. <!-- 判断当前页是否是第一页 -->
  53. <c:if test="${pageBean.currentPage==1 }">
  54. <li class="disabled">
  55. <a href="javascript:void(0);" aria-label="Previous">
  56. <span aria-hidden="true">«</span>
  57. </a>
  58. </li>
  59. </c:if>
  60. <c:if test="${pageBean.currentPage!=1 }">
  61. <li>
  62. <a href="${pageContext.request.contextPath }/productList?currentPage=${pageBean.currentPage-1}" aria-label="Previous">
  63. <span aria-hidden="true">«</span>
  64. </a>
  65. </li>
  66. </c:if>
  67. <c:forEach begin="1" end="${pageBean.totalPage }" var="page">
  68. <!-- 判断当前页 -->
  69. <c:if test="${pageBean.currentPage==page }">
  70. <li class="active"><a href="javascript:void(0);">${page}</a></li>
  71. </c:if>
  72. <c:if test="${pageBean.currentPage!=page }">
  73. <li><a href="${pageContext.request.contextPath }/productList?currentPage=${page}">${page}</a></li>
  74. </c:if>
  75. </c:forEach>
  76. <!-- 判断当前页是否是最后一页 -->
  77. <c:if test="${pageBean.currentPage==pageBean.totalPage }">
  78. <li class="disabled">
  79. <a href="javascript:void(0);" aria-label="Next">
  80. <span aria-hidden="true">»</span>
  81. </a>
  82. </li>
  83. </c:if>
  84. <c:if test="${pageBean.currentPage!=pageBean.totalPage }">
  85. <li>
  86. <a href="${pageContext.request.contextPath }/productList?currentPage=${pageBean.currentPage+1}" aria-label="Next">
  87. <span aria-hidden="true">»</span>
  88. </a>
  89. </li>
  90. </c:if>
  91. </ul>
  92. </div>
  93. <!-- 分页结束 -->
  94. <!--商品浏览记录-->
  95. <div
  96. style="width: 1210px; margin: 0 auto; padding: 0 9px; border: 1px solid #ddd; border-top: 2px solid #999; height: 246px;">
  97. <h4 style="width: 50%; float: left; font: 14px/30px 微软雅黑">浏览记录</h4>
  98. <div style="width: 50%; float: right; text-align: right;">
  99. <a href="">more</a>
  100. </div>
  101. <div style="clear: both;"></div>
  102. <div style="overflow: hidden;">
  103. <ul style="list-style: none;">
  104. <li
  105. style="width: 150px; height: 216; float: left; margin: 0 8px 0 0; padding: 0 18px 15px; text-align: center;"><img
  106. src="products/1/cs10001.jpg" width="130px" height="130px" /></li>
  107. </ul>
  108. </div>
  109. </div>
  110. <!-- 引入footer.jsp -->
  111. <jsp:include page="/footer.jsp"></jsp:include>
  112. </body>
  113. </html>

7 注意事项

1 取消超链 javascript:void(0);

2 分页查询

在这里插入图片描述

3 记住pageBean类

发表评论

表情:
评论列表 (有 0 条评论,171人围观)

还没有评论,来说两句吧...

相关阅读