Java Web 网络商城案例演示十(商品详情)
发布日期:2021-06-29 15:01:18 浏览次数:2 分类:技术文章

本文共 3002 字,大约阅读时间需要 10 分钟。

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

用户点击商品图片或名称向服务端发起请求,将商品的id发送到服务端
在ProductServlet的findProductByPid当中获取到pid,根据pid查询商品信息,将获取到的商品放入request
ProductService
ProductDao
转发到jsp/product_info.jsp

原理分析

在这里插入图片描述

步骤实现

1、准备工作 /jsp/index.jsp修改链接

${

p.pname }

¥${

p.shop_price }

在这里插入图片描述

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
findHots() throws Exception; //查询最新最热商品 List
findNews() throws Exception; //查询详情 Product findProductByPid(String pid)throws Exception;}

ProductServiceImpl

public class ProductServiceImpl implements ProductService {
ProductDao productDao = new ProductDaoImpl(); @Override public List
findHots() throws Exception {
// TODO Auto-generated method stub return productDao.findHots(); } @Override public List
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
findHots() throws Exception; List
findNews() throws Exception; Product findProductByPid(String pid)throws Exception;}

ProductDaoImpl

public class ProductDaoImpl implements ProductDao {
@Override public List
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.class)); } @Override public List
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.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.class),pid); }}
${product}:底层依次吊样个域对象上的*.getAttribute("keyName");					寻找搭配request可以获取到一个对象					$(product.pname);通过获取到的product对象去调用对象上getPname()方法的,返回值。

转载地址:https://code100.blog.csdn.net/article/details/104996603 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java Web 网络商城案例演示十一(商品分页)
下一篇:Java Web 网络商城案例演示九(首页热门商品和最新商品显示)

发表评论

最新留言

不错!
[***.144.177.141]2024年05月02日 12时39分20秒