1.GoodsDao.java
public List<Map<String, Object>> getGoodsList(int recommendType) throws SQLException { QueryRunner r = new QueryRunner(DBUtil.getDataSource()); String sql = "select g.id,g.name,g.cover,g.price,t.name typename from recommend r,goods g,type t where type=? and r.goods_id=g.id and g.type_id=t.id"; return r.query(sql,new MapListHandler(),recommendType); }
2. GoodsService.java
public List<Map<String, Object>> getNewGoodsList() { List<Map<String, Object>> list = null; try { list = gDao.getGoodsList(3); // 新品的Type值为3 } catch (SQLException e) { e.printStackTrace(); } return list; }
3. IndexServlet.java
protected void doGet(){ List<Map<String, Object>> newList = gService.getNewGoodsList(); request.setAttribute("newList", newList); }
4. index.jsp
<div class="clearfix"></div> <div class="alert alert-info">新品推荐</div> <div class="gallery-grids">
<c:forEach items="${newList }" var="g"> <div class="col-md-3 gallery-grid "> <a href="detail.action?goodid=14"> <img src="${pageContext.request.contextPath }${g.cover}" class="img-responsive" alt="${g.name }"/> </a> <div class="gallery-info"> <p> <span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span> <a href="detail.action?goodid=14">查看详情</a> </p> <a class="shop" href="javascript:;" onclick="buy(14)">立刻购买</a> <div class="clearfix"> </div> </div> <div class="galy-info"> <p>${g.typeName } > ${g.name }</p> <div class="galry"> <div class="prices"> <h5 class="item_price">¥ ${g.price }</h5> </div> <div class="clearfix"></div> </div> </div> </div> </c:forEach> </div> </div> </div>