Java Web 网络商城案例演示十二(查询类别下的商品信息)

2、原理

在这里插入图片描述

3、步骤实现

1、准备工作:

修改/jsp/header.jsp下的js里面的链接。

在这里插入图片描述

将当前页和对应商品的cid(商品id) 传入到对应的servlet

var li = “

  • ”+obj.cname+“
  • ”;

    2、在productServlet当中创建findProductByCidWithPage方法

    在这里插入图片描述

    public String findProductsByCidWithPage(HttpServletRequest request, HttpServletResponse response) throws Exception {

    //获取cid,num

    String cid = request.getParameter(“cid”);

    int curNum = Integer.parseInt(request.getParameter(“num”));

    //调用业务层功能:以分页形式查询当前类别下的商品

    //返回PageModel对象(1、当前页信息 2、分页 3、url)

    ProductService productService = new ProductServiceImpl();

    PageModel pm = productService.findProductsByCidWithPage(cid,curNum);

    //将pageModel对象放入到request当中

    request.setAttribute(“page”, pm);

    //转发到/jsp/product_list.jsp当中

    return “/jsp/product_list.jsp”;

    }

    ScalarHandler: 将单个值封装

    3、在ProductService当中创建对应的方法及其内容

    创建PageModel对象目的:计算分页参数

    统计当前分类下商品的个数 select count(*) from product where cid=?

    关联集合 select * from product where cid = ? LIMIT ?,?

    关联url

    返回所有的分页数据以及对应的页码对象

    在这里插入图片描述

    @Override

    public PageModel findProductsByCidWithPage(String cid, int curNum) throws Exception {

    //1、创建PageModel对象目的:计算分页参数

    //统计当前分类下商品的个数 select count(*) from product where cid=?

    int totalRecords = productDao.findTotalRecords(cid);

    PageModel pm = new PageModel(curNum, totalRecords, 12);

    //2、关联集合 select * from product where cid = ? LIMIT ?,?

    List list = productDao.findProductsByCidWithPage(cid,pm.getStartIndex(),pm.getPageSize());

    pm.setList(list);

    //3、关联url

    pm.setUrl(“”);

    //返回所有的分页数据(对应商品信息)以及对应的页码的对象

    return null;

    }

    4、依次在ProductDaoImpl当中实现两个方法

    findTotalRecords返回的是对应cid数据的个数

    findProductsByCidWithPage方法返回的是List对应cid的商品,cid是在category上获取的,到product上查找随意cid 的商品

    @Override

    public int findTotalRecords(String cid) throws Exception {

    String sql = “select count(*) from product where cid = ?”;

    QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());

    Long num = (Long)qr.query(sql, new ScalarHandler(),cid);//ScalarHandler: 将单个值封装

    return num.intValue();

    }

    @Override

    public List findProductsByCidWithPage(String cid, int startIndex, int pageSize) throws Exception {

    String sql = “select * from product where cid = ? limit ?,?”;

    QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());

    return qr.query(sql, new BeanListHandler(Product.class),cid,startIndex,pageSize);

    }

    4、写一个公共的分页代码,获取request当中传递过来的PageModel对象,并从其中获取对应的值

    <%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8”%>

    <%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>

    <%–分页显示的开始 --%>

    p a g e . t o t a l P a g e N u m 页 / 第 {page.totalPageNum}页/第 page.totalPageNum/{page.currentPageNum}页

    首页

    上一页

    <%–显示的页码,使用forEach遍历显示的页面 --%>

    <c:forEach begin=“ p a g e . s t a r t P a g e " e n d = " {page.startPage}" end=" page.startPage"end="{page.endPage}” var=“pagenum”>

    ${pagenum}

    </c:forEach>

    下一页

    末页

    <%–分页显示的结束–%>

    product_list.jsp当中获取值并显示

    <%@ page language=“java” contentType=“text/html; charset=UTF-8”

    pageEncoding=“UTF-8”%>

    <%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core”%>

    <!doctype html>

    商品列表

    href=“${pageContext.request.contextPath}/css/bootstrap.min.css”

    type=“text/css” />

    href=“${pageContext.request.contextPath}/css/style.css” type=“text/css” />

    <%@include file=“/jsp/header.jsp”%>

    <c:if test=“${empty page.list }”>

    暂无商品信息

    </c:if>

    <c:if test=“${not empty page.list }”>

  • 首页
  • <c:forEach items=“${page.list }” var=“p”>

    <img

    src=“ p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h / {pageContext.request.contextPath}/ pageContext.request.contextPath/{p.pimage}”

    width=“170” height=“170” style=“display: inline-block;”>

    <a href=“${pageContext.request.contextPath}/jsp/product_info.jsp”

    style=‘color: green’>${p.pname }

    商城价:¥${p.shcp_price }

    </c:forEach>

    <%@ include file=“/jsp/pageFile.jsp”%>

    </c:if>

    style=“width: 1210px; margin: 0 auto; padding: 0 9px; border: 1px solid #ddd; border-top: 2px solid #999; height: 246px;”>

  • 9
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值