web层的servlet
1 package web; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 import java.util.List; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import domain.Product; 13 import service.ProductService; 14 import vo.PageBean; 15 16 public class ProductListServlet extends HttpServlet { 17 18 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 19 ProductService service = new ProductService(); 20 // 模拟当前页为1 21 String currentPageStr = request.getParameter("currentPage"); 22 if(currentPageStr==null)currentPageStr="1"; 23 int currentPage=Integer.parseInt(currentPageStr); 24 // 模拟每页显示12条 25 int currentCount=12; 26 PageBean<Product> pageBean =null; 27 try { 28 pageBean = service.findPageBean(currentPage,currentCount); 29 } catch (SQLException e) { 30 e.printStackTrace(); 31 } 32 /** 33 * 当前页面:currentPage 34 * 当前页面的条数:current 35 * 数据总页数:totalPage 36 * 数据总条数:totalCount 37 * 每页数据集合:productList 38 */ 39 40 request.setAttribute("pageBean", pageBean); 41 request.getRequestDispatcher("/product_list.jsp").forward(request, response); 42 43 44 } 45 46 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 47 doGet(request, response); 48 } 49 }
service层
1 package service; 2 3 import java.sql.SQLException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import dao.ProductDao; 8 import domain.Product; 9 import vo.PageBean; 10 11 public class ProductService { 12 13 public List<Product> findAllProduct() throws SQLException { 14 ProductDao dao = new ProductDao(); 15 16 return dao.findAllProduct(); 17 } 18 19 public PageBean findPageBean(int currentPage,int currentCount) throws SQLException { 20 ProductDao dao = new ProductDao(); 21 22 //想办法封装pageBean并返回 23 PageBean pageBean = new PageBean(); 24 //1.当前页 private int currentPage; 25 pageBean.setCurrentPage(currentPage); 26 27 //2.当前显示条数 private int currentCount; 28 pageBean.setCurrentCount(currentCount); 29 //3.总条数 private int totalCount; 30 int totalCount = dao.getTotalCount(); 31 pageBean.setTotalCount(totalCount); 32 //4.总页数 private int totalPage; 33 int totalPage = (int)Math.ceil(1.0*totalCount/currentCount); 34 pageBean.setTotalPage(totalPage); 35 //5.从第几条数据开始 int index 36 int index = (currentPage-1)*currentCount; 37 //6.每页显示的数据 private List<T> productList = new ArrayList(); 38 List<Product> productList = dao.findProductListForPageBean(index,currentCount); 39 pageBean.setProductList(productList); 40 41 return pageBean; 42 } 43 44 }
dao层
package dao; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import domain.Product; import utils.MyDataSourceUtils; public class ProductDao { public List<Product> findAllProduct() throws SQLException { return new QueryRunner(MyDataSourceUtils.getDataSource()).query("select * from product",new BeanListHandler<Product>(Product.class)); } // 获得全部商品条数 public int getTotalCount() throws SQLException { QueryRunner runner = new QueryRunner(MyDataSourceUtils.getDataSource()); String sql = "select count(*) from product"; Long query = (Long)runner.query(sql,new ScalarHandler()); return query.intValue(); } //获取分页的商品数据 public List<Product> findProductListForPageBean(int index,int currentCount) throws SQLException { QueryRunner runner = new QueryRunner(MyDataSourceUtils.getDataSource()); String sql = "select * from product limit ?,?"; List<Product> productList=runner.query(sql, new BeanListHandler<Product>(Product.class),index,currentCount); return productList; } }
jsp页面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta name="viewport" content="width=device-width, initial-scale=1"> 8 <title>会员登录</title> 9 <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" /> 10 <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script> 11 <script src="js/bootstrap.min.js" type="text/javascript"></script> 12 <!-- 引入自定义css文件 style.css --> 13 <link rel="stylesheet" href="css/style.css" type="text/css" /> 14 15 <style> 16 body { 17 margin-top: 20px; 18 margin: 0 auto; 19 width: 100%; 20 } 21 22 .carousel-inner .item img { 23 width: 100%; 24 height: 300px; 25 } 26 </style> 27 </head> 28 29 <body> 30 31 32 <!-- 引入header.jsp --> 33 <jsp:include page="/header.jsp"></jsp:include> 34 35 36 <div class="row" style="width: 1210px; margin: 0 auto;"> 37 <div class="col-md-12"> 38 <ol class="breadcrumb"> 39 <li><a href="#">首页</a></li> 40 </ol> 41 </div> 42 <c:forEach items="${pageBean.productList}" var="product"> 43 <div class="col-md-2" style="height:250px"> 44 <a href="product_info.htm"> <img 45 src="${pageContext.request.contextPath}/${product.pimage}" 46 width="170" height="170" style="display: inline-block;"> 47 </a> 48 <p> 49 <a href="product_info.html" style='color: green'>${product.pname}</a> 50 </p> 51 <p> 52 <font color="#FF0000">商城价:¥${product.shop_price}</font> 53 </p> 54 </div> 55 56 57 58 </c:forEach> 59 60 61 62 63 </div> 64 65 <!--分页 --> 66 <div style="width: 380px; margin: 0 auto; margin-top: 50px;"> 67 <ul class="pagination" style="text-align: center; margin-top: 10px;"> 68 69 <c:if test="${pageBean.currentPage==1 }"> 70 <li class="disabled"> 71 <a href="javascript:void(0);" aria-label="Previous"> 72 <span aria-hidden="true">«</span> 73 </a> 74 </li> 75 </c:if> 76 77 <c:if test="${pageBean.currentPage!=1 }"> 78 <li > 79 <a href="${pageContext.request.contextPath }/productList?currentPage=${pageBean.currentPage-1}" aria-label="Previous"> 80 <span aria-hidden="true">«</span> 81 </a> 82 </li> 83 </c:if> 84 85 <c:forEach begin="1" end="${pageBean.totalPage}" var="page"> 86 <c:if test="${page!=pageBean.currentPage}"> 87 <li><a href="${pageContext.request.contextPath }/productList?currentPage=${page}">${page}</a></li> 88 </c:if> 89 <c:if test="${page==pageBean.currentPage}"> 90 <li class="active"><a href="javascript:void(0);">${page}</a></li> 91 </c:if> 92 93 </c:forEach> 94 <c:if test="${pageBean.currentPage!=pageBean.totalPage}"> 95 <li> 96 <a href="${pageContext.request.contextPath }/productList?currentPage=${pageBean.currentPage+1}" aria-label="Next"> 97 <span aria-hidden="true">»</span> 98 </a> 99 </li> 100 </c:if> 101 <c:if test="${pageBean.currentPage==pageBean.totalPage}"> 102 <li class="disabled"> 103 <a href="javascript:void(0);" aria-label="Next"> 104 <span aria-hidden="true">»</span> 105 </a> 106 </li> 107 </c:if> 108 109 110 111 112 <!-- <li class="disabled"><a href="#" aria-label="Previous"><span 113 aria-hidden="true">«</span></a></li> 114 <li class="active"><a href="#">1</a></li> 115 <li><a href="#">2</a></li> 116 <li><a href="#">3</a></li> 117 <li><a href="#">4</a></li> 118 <li><a href="#">5</a></li> 119 <li><a href="#">6</a></li> 120 <li><a href="#">7</a></li> 121 <li><a href="#">8</a></li> 122 <li><a href="#">9</a></li> 123 <li><a href="#" aria-label="Next"> <span aria-hidden="true">»</span> 124 </a></li> --> 125 </ul> 126 </div> 127 <!-- 分页结束 --> 128 129 <!--商品浏览记录--> 130 <div 131 style="width: 1210px; margin: 0 auto; padding: 0 9px; border: 1px solid #ddd; border-top: 2px solid #999; height: 246px;"> 132 133 <h4 style="width: 50%; float: left; font: 14px/30px 微软雅黑">浏览记录</h4> 134 <div style="width: 50%; float: right; text-align: right;"> 135 <a href="">more</a> 136 </div> 137 <div style="clear: both;"></div> 138 139 <div style="overflow: hidden;"> 140 141 <ul style="list-style: none;"> 142 <li 143 style="width: 150px; height: 216; float: left; margin: 0 8px 0 0; padding: 0 18px 15px; text-align: center;"><img 144 src="products/1/cs10001.jpg" width="130px" height="130px" /></li> 145 </ul> 146 147 </div> 148 </div> 149 150 151 <!-- 引入footer.jsp --> 152 <jsp:include page="/footer.jsp"></jsp:include> 153 154 </body> 155 156 </html>
pagebean
package vo; import java.util.ArrayList; import java.util.List; import domain.Product; public class PageBean<T> { //当前页 private int currentPage; //当前显示条数 private int currentCount; //总页数 private int totalPage; //总条数 private int totalCount; //每页显示的数据 private List<T> productList = new ArrayList(); public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getCurrentCount() { return currentCount; } public void setCurrentCount(int currentCount) { this.currentCount = currentCount; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List<T> getProductList() { return productList; } public void setProductList(List<T> productList) { this.productList = productList; } }