java web 网上商城_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.给每个商品添加超链接  yy

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条。

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.代码区

95d35ea1433d3f7058e80f27c6ec7684.png

0a10a32440e94353e47eea3e3855cf04.png

849aa7a532c4fbc035b7bef0e98cf95c.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.itheima.web.servlet;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.itheima.domain.PageBean;importcom.itheima.domain.Product;importcom.itheima.service.ProductService;importcom.itheima.service.impl.ProductServiceImpl;importcom.itheima.web.servlet.base.BaseServlet;/*** 前台商品模块*/

public class ProductServlet extendsBaseServlet {private static final long serialVersionUID = 1L;/*** 分类商品分页展示*/

public String findByPage(HttpServletRequest request, HttpServletResponse response) throwsServletException, 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 = newProductServiceImpl();

PageBean 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";

}/*** 商品详情

*@paramrequest

*@paramresponse

*@return*@throwsServletException

*@throwsIOException*/

public String getById(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {try{//1.获取pid

String pid = request.getParameter("pid");//2.调用service获取单个商品 参数:pid 返回值:product

ProductService ps =newProductServiceImpl();

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.itheima.dao.impl;importjava.util.List;importorg.apache.commons.dbutils.QueryRunner;importorg.apache.commons.dbutils.handlers.BeanHandler;importorg.apache.commons.dbutils.handlers.BeanListHandler;importorg.apache.commons.dbutils.handlers.ScalarHandler;importcom.itheima.constant.Constant;importcom.itheima.dao.ProductDao;importcom.itheima.domain.PageBean;importcom.itheima.domain.Product;importcom.itheima.utils.DataSourceUtils;public class ProductDaoImpl implementsProductDao {

@Override/*** 查询热门*/

public List findHot() throwsException {

QueryRunner qr= newQueryRunner(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 findNew() throwsException {

QueryRunner qr= newQueryRunner(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) throwsException {

QueryRunner qr= newQueryRunner(DataSourceUtils.getDataSource());

String sql= "select * from product where pid = ? limit 1";return qr.query(sql, new BeanHandler<>(Product.class), pid);

}

@Override/*** 查询当前页数据*/

public List findByPage(PageBean pb, String cid) throwsException {

QueryRunner qr= newQueryRunner(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) throwsException {return ((Long)new QueryRunner(DataSourceUtils.getDataSource()).query("select count(*) from product where cid = ? and pflag = ?", newScalarHandler(), cid,Constant.PRODUCT_IS_UP)).intValue();

}

}

ProductDaoImpl

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.itheima.dao.impl;importjava.util.List;importorg.apache.commons.dbutils.QueryRunner;importorg.apache.commons.dbutils.handlers.BeanListHandler;importcom.itheima.dao.CategoryDao;importcom.itheima.domain.Category;importcom.itheima.utils.DataSourceUtils;public class CategoryDaoImpl implementsCategoryDao {

@Override/*** 查询所有分类*/

public List findAll() throwsException {

QueryRunner qr= newQueryRunner(DataSourceUtils.getDataSource());

String sql= "select * from category";return qr.query(sql, new BeanListHandler<>(Category.class));

}

}

CategoryDaoImpl 查询所有分类

  • 1
    点赞
  • 6
    收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MasterPa

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值