12_首页显示热门商品和最新商品

首页显示热门商品和最新商品

  • 步骤分析
  • 准备工作
  • 代码实现

1)步骤分析

  1. 在页面加载的时候查询最新商品和热门商品即可。
  2. 在indexServlet的index方法中实现就可以了
    查询的结果为两个集合list,将两个list放入request域中,请求转发到index.jsp即可。
  3. 在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">&yen;${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">&yen;${newPro.shopPrice}</font></p>
	</div>
</c:forEach> 
④ 测试

热门商品:
在这里插入图片描述
最新商品:
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

robona

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值