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.代码区
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
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
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 查询所有分类