Java Web 网络商城案例演示十(商品详情)
用户点击商品图片或名称向服务端发起请求,将商品的id发送到服务端
在ProductServlet的findProductByPid当中获取到pid,根据pid查询商品信息,将获取到的商品放入request
ProductService
ProductDao
转发到jsp/product_info.jsp
原理分析
步骤实现
1、准备工作 /jsp/index.jsp修改链接
<c:forEach items="${hots }" var="p">
<div class="col-md-2"
style="text-align: center; height: 200px; padding: 10px 0px;">
<a href="${pageContext.request.contextPath}/ProductServlet?method=findProductByPid&pid=${p.pid}"> <img
src="${pageContext.request.contextPath}/${p.pimage}" width="130" height="130"
style="display: inline-block;">
</a>
<p>
<a href="${pageContext.request.contextPath}/ProductServlet?method=findProductByPid&pid=${p.pid}" style='color: #666'>${p.pname }</a>
</p>
<p>
<font color="#E4393C" style="font-size: 16px">¥${p.shop_price }</font>
</p>
</div>
</c:forEach>
ProductServlet
public class ProductServlet extends BaseServlet {
// findProductByPid
public String findProductByPid(HttpServletRequest request, HttpServletResponse response) throws Exception {
String pid = request.getParameter("pid");
System.out.println("pid="+pid);
ProductService productService = new ProductServiceImpl();
Product product = productService.findProductByPid(pid);
request.setAttribute("product", product);
return "/jsp/product_info.jsp";
}
}
ProductService
public interface ProductService {
//查询最新商品
List<Product> findHots() throws Exception;
//查询最新最热商品
List<Product> findNews() throws Exception;
//查询详情
Product findProductByPid(String pid)throws Exception;
}
ProductServiceImpl
public class ProductServiceImpl implements ProductService {
ProductDao productDao = new ProductDaoImpl();
@Override
public List<Product> findHots() throws Exception {
// TODO Auto-generated method stub
return productDao.findHots();
}
@Override
public List<Product> findNews() throws Exception {
// TODO Auto-generated method stub
return productDao.findNews();
}
@Override
public Product findProductByPid(String pid) throws Exception {
// TODO Auto-generated method stub
return productDao.findProductByPid(pid);
}
}
ProductDao
public interface ProductDao {
List<Product> findHots() throws Exception;
List<Product> findNews() throws Exception;
Product findProductByPid(String pid)throws Exception;
}
ProductDaoImpl
public class ProductDaoImpl implements ProductDao {
@Override
public List<Product> findHots() throws Exception {
// TODO Auto-generated method stub
String sql = "SELECT * FROM product WHERE pflag=0 AND is_hot=1 ORDER BY pdate DESC LIMIT 0,9;";
QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
return qr.query(sql, new BeanListHandler<Product>(Product.class));
}
@Override
public List<Product> findNews() throws Exception {
// TODO Auto-generated method stub
String sql = "SELECT * FROM product WHERE pflag=0 ORDER BY pdate DESC LIMIT 0,9";
QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
return qr.query(sql, new BeanListHandler<Product>(Product.class));
}
@Override
public Product findProductByPid(String pid) throws Exception {
// TODO Auto-generated method stub
String sql = "select * from product where pid=?";
QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
return qr.query(sql, new BeanHandler<Product>(Product.class),pid);
}
}
${product}:底层依次吊样个域对象上的*.getAttribute("keyName");
寻找搭配request可以获取到一个对象
$(product.pname);通过获取到的product对象去调用对象上getPname()方法的,返回值。