首页显示热门商品和最新商品
- 步骤分析
- 准备工作
- 代码实现
1)步骤分析
- 在页面加载的时候查询最新商品和热门商品即可。
- 在indexServlet的index方法中实现就可以了
查询的结果为两个集合list,将两个list放入request域中,请求转发到index.jsp即可。 - 在index.jsp中展示数据。
2)准备工作
① 数据库数据和表
CREATE TABLE `product` (
`pid` varchar(32) NOT NULL,
`pname` varchar(50) DEFAULT NULL,
`market_price` double DEFAULT NULL,
`shop_price` double DEFAULT NULL,
`pimage` varchar(200) DEFAULT NULL,
`pdate` date DEFAULT NULL,
`is_hot` int(11) DEFAULT NULL,
`pdesc` varchar(255) DEFAULT NULL,
`pflag` int(11) DEFAULT NULL,
`cid` varchar(32) DEFAULT NULL,
PRIMARY KEY (`pid`),
KEY `sfk_0001` (`cid`),
CONSTRAINT `sfk_0001` FOREIGN KEY (`cid`) REFERENCES `category` (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
② 创建商品实体
在entity中创建实体类Product:
public class Product implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4332584225565337619L;
/**
* 商品ID
*/
private String pid;
/**
* 商品名称
*/
private String pname;
/**
* 市场价
*/
private Double marketPrice;
/**
* 商城价
*/
private Double shopPrice;
/**
* 图片路径
*/
private String pimage;
/**
*
*/
private Date pdate;
/**
* 是否热门 1:热门 0:不热门
*/
private Integer isHot;
/**
* 描述
*/
private String pdesc;
/**
* 是否下架 1:下架 0:未下架
*/
private Integer pflag;
/**
* 属于哪个分类 把Integer的cid变为Category类型
*/
private Category category;
//getter和sertter略写
}
③ 创建Dao层和Service层接口和实现类
3)代码实现
①更改IndexServlet中的index方法
因为在head.jsp中动态加载了分类展示,所以把分类的查询和绑定删除掉,直接转发即可:
private void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
//请求转发
request.getRequestDispatcher("/jsp/index.jsp").forward(request, response);
}
然后查询最新商品和热门商品 将他们转发到首页
private void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
//1、最新商品
List<Product> newList=productService.findNew();
//2、热门商品
List<Product> hotList=productService.findHot();
//将两个集合绑定到request中转发
request.setAttribute("newList", newList);
request.setAttribute("hotList", hotList);
//请求转发
request.getRequestDispatcher("/jsp/index.jsp").forward(request, response);
}
② 完成dao和service
这里只展示ProductDaoImpl:
public class ProductDaoImpl implements ProductDao{
//QueryRunner
private QueryRunner qr=new QueryRunner(DBUtil.getDataSource());
@Override
public List<Product> findNew() throws Exception {
//查询最新的9个商品 按照时间排序查询前9个
String sql="select pid,pname,market_price marketPrice,shop_price shopPrice,pimage,"
+ "pdate,is_hot isHot,pdesc,pflag,cid from `product` order by pdate limit 9";
return qr.query(sql, new BeanListHandler<>(Product.class));
}
@Override
public List<Product> findHot() throws Exception {
String sql="select pid,pname,market_price marketPrice,shop_price shopPrice,"
+ "pimage,pdate,is_hot isHot,pdesc,pflag,cid from `product` where is_hot = 1 order by pdate limit 9";
return qr.query(sql, new BeanListHandler<>(Product.class));
}
}
③ 在index.jsp上完成商品展示
最热商品:
<c:forEach items="${hotList }" var="hotPro">
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="${path}/${hotPro.pimage}" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>${hotPro.pname }</a></p>
<p><font color="#E4393C" style="font-size:16px">¥${hotPro.shopPrice}</font></p>
</div>
</c:forEach>
最新商品:
<c:forEach items="${newList }" var="newPro">
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="${path}/${newPro.pimage}" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>${newPro.pname }</a></p>
<p><font color="#E4393C" style="font-size:16px">¥${newPro.shopPrice}</font></p>
</div>
</c:forEach>
④ 测试
热门商品:
最新商品: