###Jsp+Servlet购物商城day02.2:首页显示 最新和热门商品

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

功能入口:IndexServlet (进入首页之前,IndexServlet 查出所有 最新和热门商品,带到首页显示)

----------热门:product表 有isHot字段,对应有属性;

----------最新:按pdate字段降序查询limit ?(默认从第一个开始查询?个)。====(没有limit ?,查出来全部,太多,不合适)

功能出口:首页显示。


功能入口:IndexServlet (进入首页之前,IndexServlet 查出所有 最新和热门商品,带到首页显示)

代码:

package cn.itcast.servlet;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.domain.Product;
import cn.itcast.service.ProductService;
import cn.itcast.service.impl.ProductServiceImpl;

public class IndexServlet extends BaseServlet {

	@Override
	public String execute(HttpServletRequest request, HttpServletResponse response) {
		ProductService ps = new ProductServiceImpl();
		try {
			List<Product> hotList = ps.findHot();
			List<Product> newList = ps.findNew();
			
			request.setAttribute("hotList", hotList);
			request.setAttribute("newList", newList);
			request.getRequestDispatcher("index.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "index.jsp";
	}

}

service

public class ProductServiceImpl implements ProductService {
	public List<Product> findHot() throws SQLException {
		ProductDao pd  = new ProductDaoImpl();
		 List<Product> hotList = pd.findHot();
		return hotList;
	}

	public List<Product> findNew() throws SQLException {
		ProductDao pd  = new ProductDaoImpl();
		 List<Product> newList = pd.findNew();
		return newList;
	}
}


dao

public class ProductDaoImpl implements ProductDao {

	@Override
	public List<Product> findHot() throws SQLException {
		QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
		String sql = "select * from product where is_hot = ? limit ?";//省略startIndex。默认前pageSize个
		Object[] params = {1,9};
		List<Product> list = qr.query(sql , new BeanListHandler<>(Product.class),params);
		return list;
	}

	@Override
	public List<Product> findNew() throws SQLException {
		QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
		String sql = "select * from product  order by pdate desc limit ?";//==###=省略startIndex。默认前pageSize个

		List<Product> list = qr.query(sql , new BeanListHandler<>(Product.class));
		return list;
	}
}

 功能出口:首页显示:

<!--
            	描述:商品显示
            -->
			<div class="container-fluid">
				<div class="col-md-12">
					<h2>热门商品  <img src="img/title2.jpg"/></h2>
				</div>
				<div class="col-md-2" style="border:1px solid #E7E7E7;border-right:0;padding:0;">
					<img src="${pageContext.request.contextPath}/products/hao/big01.jpg" width="205" height="404" style="display: inline-block;"/>
				</div>
				<div class="col-md-10">
					<div class="col-md-6" style="text-align:center;height:200px;padding:0px;">
						<a href="product_info.htm">
							<img src="${pageContext.request.contextPath}/products/hao/middle01.jpg" width="516px" height="200px" style="display: inline-block;">
						</a>
					</div>
					
					<c:forEach var="pro" items="${hotList }">
						<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
							<a href="product_info.htm">
								<img src="${pageContext.request.contextPath}/${pro.pimage}" width="130" height="130" style="display: inline-block;">
							</a>
							<p><a href="product_info.html" style='color:#666'>${pro.pname }</a></p>
							<p><font color="#E4393C" style="font-size:16px">¥${pro.shop_price }</font></p>
						</div>
					
					</c:forEach>
				</div>
			</div>
			<!--
            	描述:广告部分
            -->
            <div class="container-fluid">
				<img src="${pageContext.request.contextPath}/products/hao/ad.jpg" width="100%"/>
			</div>
			<!--
            	作者:ci2713@163.com
            	时间:2015-12-30
            	描述:商品显示
            -->
			<div class="container-fluid">
				<div class="col-md-12">
					<h2>最新商品  <img src="img/title2.jpg"/></h2>
				</div>
				<div class="col-md-2" style="border:1px solid #E7E7E7;border-right:0;padding:0;">
					<img src="${pageContext.request.contextPath}/products/hao/big01.jpg" width="205" height="404" style="display: inline-block;"/>
				</div>
				<div class="col-md-10">
					<div class="col-md-6" style="text-align:center;height:200px;padding:0px;">
						<a href="product_info.htm">
							<img src="${pageContext.request.contextPath}/products/hao/middle01.jpg" width="516px" height="200px" style="display: inline-block;">
						</a>
					</div>
					<c:forEach var="pro" items="${newList }">
						<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
							<a href="product_info.htm">
								<img src="${pageContext.request.contextPath}/${pro.pimage}" width="130" height="130" style="display: inline-block;">
							</a>
							<p><a href="product_info.html" style='color:#666'>${pro.pname }</a></p>
							<p><font color="#E4393C" style="font-size:16px">¥${pro.shop_price }</font></p>
						</div>
					
					</c:forEach>
					
				</div>
			</div>			




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值