新建一个jsp,命名为:goodsrecommendlist.jsp,其内部和goods_list差不多,因为查询的方式没太大的区别:
在GoodsDao中写入查询代码:
public List<Goods> selectGoodsRecommend(int type,int pageNo,int pageSize) throws SQLException{ QueryRunner r = new QueryRunner(DBUtil.getDataSource()); String sql="select g.id,g.name,g.cover,g.image1,g.image2,g.intro,g.price,g.stock from goods g,recommend r where g.id=r.goods_id and r.type=? limit ?,?"; return r.query(sql, new BeanListHandler<Goods>(Goods.class),type, (pageNo-1)*pageSize,pageSize ); } public int selectGoodsRecommendCount(int type) throws SQLException{//表明查询的是热销总数还是新品 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); String sql="select count(*) from recommend where type=?"; return r.query(sql, new ScalarHandler<Long>(),type ).intValue();
在GoodsService中加入设置当前页以及类目及异常处理:
public Page getGoodsRecommendPage(int type ,int pageNo) { Page p=new Page(); p.setPageNo(pageNo); int totalCount=0; try { totalCount= gDao.selectGoodsRecommendCount(type); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } p.setPageSizeAndTotalCount(8, totalCount);//自动计算总页数 List list=null; try { list = gDao.selectGoodsRecommend(type, pageNo, 8); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } p.setList(list);//分页模型中的list是一个object return p; }
在servlet层中创建一个servlet,命名为:GoodsRecommendListServlet
package com.guiyan.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.guiyan.model.Page; import com.guiyan.service.GoodsService; /** * Servlet implementation class GoodsRecommendListServlet */ @WebServlet("/goodsrecommend_list") public class GoodsRecommendListServlet extends HttpServlet { private GoodsService gService=new GoodsService(); /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //得到的是一个推荐的类型 int type=Integer.parseInt(request.getParameter("type") ) ; //表示当前查询的页数 int pageNo = 1; if(request.getParameter("pageNo") != null) { pageNo=Integer.parseInt(request.getParameter("pageNo")) ; } Page p=gService.getGoodsRecommendPage(type, pageNo); request.setAttribute("p", p); request.setAttribute("t", type); request.getRequestDispatcher("goodsrecommend_list.jsp").forward(request, response); } }
最后将通过goodsrecommendlist.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> <html> <head> <title>首页</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link type="text/css" rel="stylesheet" href="css/bootstrap.css"> <link type="text/css" rel="stylesheet" href="css/style.css"> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/bootstrap.min.js"></script> <script type="text/javascript" src="js/simpleCart.min.js"></script> <script type="text/javascript" src="layer/layer.js"></script> <script type="text/javascript" src="js/cart.js"></script> </head> <body> <!-- header --> <jsp:include page="/header.jsp"></jsp:include> <!--//header--> <!--products--> <div class="products"> <div class="container"> <h2><c:choose><c:when test="${t==2 }">热销商品</c:when><c:otherwise>新品上市</c:otherwise></c:choose></h2> <div class="col-md-12 product-model-sec"> <c:forEach items="${p.list}" var="g"> <div class="product-grid"> <a href="detail.action?goodid=6"> <div class="more-product"><span> </span></div> <div class="product-img b-link-stripe b-animate-go thickbox"> <img src="${pageContext.request.contextPath }${g.cover}" class="img-responsive" alt="${g.name }" width="240" height="240"> <div class="b-wrapper"> <h4 class="b-animate b-from-left b-delay03"> <button>查看详情</button> </h4> </div> </div> </a> <div class="product-info simpleCart_shelfItem"> <div class="product-info-cust prt_name"> <h4>${g.name}</h4> <span class="item_price">¥${g.price}</span> <input type="button" class="item_add items" value="加入购物车" onclick="buy(6)"> <div class="clearfix"> </div> </div> </div> </div> </c:forEach> <div class="clearfix"> </div> </div> <div> <div style='text-align:center;'> <a class='btn btn-info' <c:if test="${p.pageNo==1 }">disabled</c:if> <c:if test="${p.pageNo!=1 }">href="${pageContext.request.contextPath }/goodsrecommend_list?pageNo=1&type=${t}"</c:if>>首页</a> <a class='btn btn-info' <c:if test="${p.pageNo==1 }">disabled</c:if> <c:if test="${p.pageNo!=1 }">href="${pageContext.request.contextPath }/goodsrecommend_list?pageNo=${p.pageNo-1}&type=${t}"</c:if>>上一页</a> <h3 style='display:inline;'>[${p.pageNo }/${p.totalPage }]</h3> <h3 style='display:inline;'>[${p.totalCount }]</h3> <a class='btn btn-info' <c:if test="${p.totalPage==0 || p.pageNo==p.totalPage }">disabled</c:if> <c:if test="${p.pageNo!=p.totalPage }">href="${pageContext.request.contextPath }/goodsrecommend_list?pageNo=${p.pageNo+1}&type=${t}"</c:if>>下一页</a> <a class='btn btn-info' <c:if test="${p.totalPage==0 || p.pageNo==p.totalPage }">disabled</c:if> <c:if test="${p.pageNo!=p.totalPage }">href="${pageContext.request.contextPath }/goodsrecommend_list?pageNo=${p.totalPage}&type=${t}"</c:if>>尾页</a> <input type='text' class='form-control' style='display:inline;60px;' value=''/><a class='btn btn-info' href='javascript:void(0);' onclick='location.href="${pageContext.request.contextPath }/goods_list?id=${id}&pageNo="+(this.previousSibling.value)'>GO</a> </div> <%-- <jsp:include page="/page.jsp"> <jsp:param value="/goods_list" name="url"/> <jsp:param value="&id=${id}" name="param"/> </jsp:include> --%> </div> </div> </div> <!--//products--> <jsp:include page="/footer.jsp"></jsp:include> </body> </html>
点击热销与新品的展示效果:
完善分页:
当想要跳转某一页的时候,可以点击Go按钮就可以实现哦!!效果如下所示:
将goods_list.jsp中的这一句:
<input type='text' class='form-control' style='display:inline;60px;' value=''/><a class='btn btn-info' href='javascript:void(0);' onclick='location.href="${pageContext.request.contextPath }/goods_list?id=${id}&pageNo="+(this.previousSibling.value)'>GO</a>
复制到goodsrecommmend_list.jsp中,再进行修改为:
<input type='text' class='form-control' style='display:inline;60px;' value=''/><a class='btn btn-info' href='javascript:void(0);' onclick='location.href="${pageContext.request.contextPath }/goodsrecommend_list?type=${t}&pageNo="+(this.previousSibling.value)'>GO</a>