商品详情
程序员文章站
2022-04-24 23:44:22
...
要求
用户点击商品图片,名称向服务器端发出请求,将商品id发送到服务端
步骤
- ProductServlet下编写findProductByPid方法
public String findProductByPid(HttpServletRequest request, HttpServletResponse response) throws Exception {
//获取商品pid
String pid = request.getParameter("pid");
//根据商品pid查询商品信息
ProductService productService = new ProductServiceImp();
Product product = productService.findProductByPid(pid);
System.out.println(product);
//将获取到的商品放入request
request.setAttribute("product", product);
//转发到/jsp/product_info.jsp
return "/jsp/product_info.jsp";
}
- service层调用dao层
ProductDao productDao = (ProductDao) BeanFactory.createObject("ProductDao");
@Override
public Product findProductByPid(String pid) throws Exception {
return productDao.findProductByPid(pid);
}
- DAO层查询数据库
@Override
public Product findProductByPid(String pid) {
String sql="select * from product where pid=?";
return template.queryForObject(sql, new BeanPropertyRowMapper<>(Product.class),pid);
}
- product_info.jsp
<div class="row">
<div style="margin:0 auto;width:950px;">
<div class="col-md-6">
<img style="opacity: 1;width:400px;height:350px;" title="" class="medium"
src="${pageContext.request.contextPath}/${product.pimage}"/>
</div>
<div class="col-md-6">
<div><strong>${product.pname}</strong></div>
<div style="border-bottom: 1px dotted #dddddd;width:350px;margin:10px 0 10px 0;">
<div>编号:${product.pid}</div>
</div>
<div style="margin:10px 0 10px 0;">商城价: <strong
style="color:#ef0101;">¥:${product.shop_price}元/份</strong> 市场价:
<del>¥${product.market_price}元/份</del>
</div>
<div style="margin:10px 0 10px 0;">促销: <a target="_blank" title="限时抢购 (2014-07-30 ~ 2015-01-01)" style="background-color: #f07373;">限时抢购</a></div>
<div style="padding:10px;border:1px solid #e7dbb1;width:330px;margin:15px 0 10px 0;;background-color: #fffee6;">
<div style="margin:5px 0 10px 0;">白色</div>
<div style="border-bottom: 1px solid #faeac7;margin-top:20px;padding-left: 10px;">购买数量:
<input id="quantity" name="num" value="1" maxlength="4" size="10" type="text">
<input name="pid" value="${product.pid}" type="hidden">
<input name="ranStr" value="${ranStr}" type="hidden">
</div>
<div style="margin:20px 0 10px 0;;text-align: center;">
<%--加入到购物车 --%>
<a href="javascript:void(0)" onclick="submit()">
<input style="background: url('${pageContext.request.contextPath}/img/product.gif') no-repeat scroll 0 -600px rgba(0, 0, 0, 0);height:36px;width:127px;"
value="加入购物车" type="button">
</a> 收藏商品
</div>
</div>
</div>
</div>
<div class="clear"></div>
<div style="width:950px;margin:0 auto;">
<div style="background-color:#d3d3d3;width:930px;padding:10px 10px;margin:10px 0 10px 0;">
<strong>商品介绍</strong>
<span>${product.pdesc}</span>
</div>
</div>
</div>