展示商品的详情
- 步骤分析
- 代码实现
1)步骤分析
- 在首页上点击每个商品:< a href="${path}/product/particulars?pid=xxx">…</ a>
- 编写particulars方法
- 获取商品id
- 调用service,调用dao 返回值Product对象
- 将返回对象绑定到request转发给product_info.jsp
2)代码实现
① 更改连接路径
② 创建ProductServlet,完成particulars详情方法
private void particulars(HttpServletRequest request, HttpServletResponse response) throws Exception{
//1、获取商品Id
String pid=request.getParameter("pid");
//2、调用Service
Product product=productService.getById(pid);
//3、将结果绑定到request中
request.setAttribute("product", product);
//请求转发
request.getRequestDispatcher("/jsp/product_info.jsp").forward(request, response);
}
③ 完成dao和service
这里只展示ProductDaoImpl:
@Override
public Product getById(String pid) throws Exception {
String sql="select pid,pname,market_price marketPrice,shop_price shopPrice,"
+ "pimage,pdate,is_hot isHot,pdesc,pflag,cid from `product` "
+ "where pid="+pid;
return qr.query(sql, new BeanHandler<>(Product.class));
}
④ 修改product_info.jsp完成界面展示
略: