Java Web 网络商城案例演示十(商品详情)

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">&yen;${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()方法的,返回值。
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员猫爪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值