首页显示 最新和热门商品。
功能入口: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;
}
}
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>