zoukankan      html  css  js  c++  java
  • JavaWeb项目--网上商城 (6-2)

     day 2 

     1.模块详解

     案例1-前台分类信息展示
                        需求: 访问任意页面的时候,都需要将分类的信息展示出来
                        技术分析:包含(静态包含和动态包含)  静态包含后台只生成一个class文件,而动态包含生成多个class文件

                               AJAX步骤分析:
                                       1.创建分类表category表    `cid` 类别id    `cname`类别名字

                                       2.抽取所有页面上 logo 和 菜单部分(head.jsp) 页面加载的时候 编写函数  @include file="/jsp/head.jsp" %>     $(function(){}  jquery加载事件   发送ajax请求 $.post(url,params,fn,type);
                                           url:/store/category          params: method=findAll      fn:将返回值遍历,每一个分类封装成li标签,插入到ul标签内部      type:json
                                       3、编写categoryservlet,继承baseservlet,编写findAll方法

                                       4、调用service,查询所有的分类, categoryservice中的操作   调用dao,获取所有的分类   将list转成json返回
                                       5、在所有的页面里将 head.jsp 包含进去  获取返回值   遍历返回值  

                                       6、 每一个分类封装成li标签,插入到ul标签内部   
                                       7、修改service层的代码  获取的时候,去redis中获取,   若获取到了返回   若没有获取到,先去mysql数据库中查询出来,将list转成json放入redis中即可

       案例2   最新商品和热门商品展示
                            需求:  访问首页的时候,需要将最新商品和热门商品展示出来.
                            技术:  方式1:ajax异步  方式2:同步    使用同步步骤分析(请求转发)
                                          1 .创建商品表product :    pid商品id      pname   商品名  market_price 市场价格   shop_price商场价格  pimage图片路径   pdate上架时间    

                                                                                   is_hot是否热门   pdesc商品明细信息     pflag 物理删除状态  0未下架 1下架     cid  分类的id 外键

                                          2. 访问项目首页,请求转发indexservlet      indexservlet中使用默认index处理               

                                              调用productservice查询热门商品和最新商品, 每一个都返回一个list   将两个list放入request域中,请求转发到 /jsp/index.jsp

                                         3..在页面上将数据遍历出来

        案例3-单个商品详情
                            需求:     在首页上点击每个商品,将这个商品的详细信息展示在页面上(product_info.jsp)
                            步骤分析:  

                                       1.给每个商品添加超链接  <a href="/store/product?method=getById&pid=xxx">yy</a>
                                       2.编写productservlet,继承baseservlet,编写getById  获取商品的pid  调用service获取一个商品 返回值:product   请求转发到product_info.jsp
                                       3.service ,dao
                                      4.在product_info.jsp将商品展示
    案例4-分类商品的分页展示
                            需求:  点击菜单栏上某一个分类的时候,将该分类下的商品,分页展示出来(默认第一页)
                      技术分析:
                                       分页   页面上需要的数据    当前页数据   当前页   总页数  总记录数  每页显示的条数   limit m,n   limit (当前页-1)*每页显示的条数,每页显示的条数  

                        limit m,n    语法  /*当没有指定位置偏移量时,只取4条时,可以这样写*/  SELECT FROM YourTableName LIMIT 4;

                                                     其中m是指记录开始的index,从0开始,表示第一条记录,n是指从第m+1条开始,取n条。

                      步骤分析:      1.  修改head.jsp上的每个分类的超链接  <a href="/store/product?method=findByPage&pageNumber=1&cid=xxx">

                                           2.在cateservlet中编写findByPage方法  获取pagenumber  获取cid  设置pageSize  调用service获取分页的数据 返回值:PageBean
                                           将pagebean放入request域中,请求转发 /jsp/product_list.jsp
                                           3.编写service:   返回值:pagebean  创建一个pagebean  设置当前页需要的数据  调用dao  设置总记录数  调用dao
                                           4.dao
                                           5.在jsp/product_list.jsp上展示商品

    2.代码区
       

    package com.itheima.web.servlet;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.itheima.domain.PageBean;
    import com.itheima.domain.Product;
    import com.itheima.service.ProductService;
    import com.itheima.service.impl.ProductServiceImpl;
    import com.itheima.web.servlet.base.BaseServlet;
    
    /**
     * 前台商品模块
     */
    public class ProductServlet extends BaseServlet {
        private static final long serialVersionUID = 1L;
    
        /**
         * 分类商品分页展示
         */
        public String findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            try {
                //1.获取pagenumber cid  设置pagesize
                /*String parameter = request.getParameter("pageNumber");*/
                int pageNumber = 1;
                
                try { 
                    pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
                } catch (NumberFormatException e) {
                }
                
                int pageSize = 12;
                String cid = request.getParameter("cid");
                
                //2.调用service 分页查询商品 参数:3个, 返回值:pagebean
                ProductService ps = new ProductServiceImpl();
                PageBean<Product> bean=ps.findByPage(pageNumber,pageSize,cid);
                //3.将pagebean放入request中,请求转发 product_list.jsp
                request.setAttribute("pb", bean);
            } catch (Exception e) {
                request.setAttribute("msg", "分页查询失败");
                return "/jsp/msg.jsp";
            }
            return "/jsp/product_list.jsp";
        }
        
        /**
         * 商品详情
         * @param request
         * @param response
         * @return
         * @throws ServletException
         * @throws IOException
         */
        public String getById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            try {
                //1.获取pid
                String pid = request.getParameter("pid");
                
                //2.调用service获取单个商品 参数:pid 返回值:product
                ProductService ps =new ProductServiceImpl();
                Product pro=ps.getById(pid);
                
                //3.将product放入request域中,请求转发 /jsp/product_info.jsp
                request.setAttribute("bean", pro);
            } catch (Exception e) {
                request.setAttribute("msg", "查询单个商品失败");
                return "/jsp/msg.jsp";
            }
            
            return "/jsp/product_info.jsp";
        }
    }
    ProductServlet
    package com.itheima.dao.impl;
    
    import java.util.List;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanHandler;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    import org.apache.commons.dbutils.handlers.ScalarHandler;
    
    import com.itheima.constant.Constant;
    import com.itheima.dao.ProductDao;
    import com.itheima.domain.PageBean;
    import com.itheima.domain.Product;
    import com.itheima.utils.DataSourceUtils;
    
    public class ProductDaoImpl implements ProductDao {
    
        @Override
        /**
         * 查询热门
         */
        public List<Product> findHot() throws Exception {
            QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
            String sql = "select * from product where is_hot = ? and pflag = ? order by pdate desc limit 9";
            return qr.query(sql, new BeanListHandler<>(Product.class), Constant.PRODUCT_IS_HOT,Constant.PRODUCT_IS_UP);
        }
    
        @Override
        /**
         * 查询最新
         */
        public List<Product> findNew() throws Exception {
            QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
            String sql = "select * from product where pflag = ? order by pdate desc limit 9";
            return qr.query(sql, new BeanListHandler<>(Product.class),Constant.PRODUCT_IS_UP);
        }
    
        @Override
        /**
         * 查询单个商品
         */
        public Product getById(String pid) throws Exception {
            QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
            String sql = "select * from product where pid = ? limit 1";
            return qr.query(sql, new BeanHandler<>(Product.class), pid);
            
        }
    
        @Override
        /**
         * 查询当前页数据
         */
        public List<Product> findByPage(PageBean<Product> pb, String cid) throws Exception {
            QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
            String sql = "select * from product where cid = ? and pflag = ? order by pdate desc limit ?,?";
            return qr.query(sql, new BeanListHandler<>(Product.class), cid,Constant.PRODUCT_IS_UP,pb.getStartIndex(),pb.getPageSize());
        }
    
        @Override
        /**
         * 获取总记录数
         */
        public int getTotalRecord(String cid) throws Exception {
            return ((Long)new QueryRunner(DataSourceUtils.getDataSource()).query("select count(*) from product where cid = ? and pflag = ?", new ScalarHandler(), cid,Constant.PRODUCT_IS_UP)).intValue();
        }
    
    }
    ProductDaoImpl
    package com.itheima.dao.impl;
    
    import java.util.List;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    
    import com.itheima.dao.CategoryDao;
    import com.itheima.domain.Category;
    import com.itheima.utils.DataSourceUtils;
    
    public class CategoryDaoImpl implements CategoryDao {
    
        @Override
        /**
         * 查询所有分类
         */
        public List<Category> findAll() throws Exception {
            QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
            String sql = "select * from category";
            return qr.query(sql, new BeanListHandler<>(Category.class));
        }
    
    }
    CategoryDaoImpl 查询所有分类
  • 相关阅读:
    文件下载和进度显示
    响应
    log4j2-2.13.0版本安装
    maven私服nexus仓库3.24.0版本搭建
    window下MYSQL定时备份表库的BAT
    JBoss7.3.0EAP版本安装
    jetbrains-IDEA2020版本插件搜索以及官方汉化和其他插件安装介绍
    Jenkins迁移job插件Job Import Plugin
    Appium下出现Original error: pkg.... 解决办法
    IIS10下部署.NetCore站点出现出现 HTTP 错误 500.19,错误代码:0x8007000d及一些问题
  • 原文地址:https://www.cnblogs.com/ou-pc/p/7880429.html
Copyright © 2011-2022 走看看