天天看點

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層接口和實作類

12_首頁顯示熱門商品和最新商品首頁顯示熱門商品和最新商品

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> 
           

④ 測試

熱門商品:

12_首頁顯示熱門商品和最新商品首頁顯示熱門商品和最新商品

最新商品:

12_首頁顯示熱門商品和最新商品首頁顯示熱門商品和最新商品