jsp实现传智书城商品目录

一、案例演示

点击全部商品目录出现全部商品,点击单个商品出现商品详细信息
在这里插入图片描述
在这里插入图片描述

二、实现步骤

1.menu_search.jsp代码

<div id="divmenu">
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=文学">文学</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=生活">生活</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=计算机">计算机</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=外语">外语</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=经营">经管</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=励志">励志</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=社科">社科</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=学术">学术</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=少儿">少儿</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=艺术">艺术</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=原版">原版</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=科技">科技</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=考试">考试</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=生活百科">生活百科</a>
		<a href="${pageContext.request.contextPath}/client/logins?act=show" style="color:#b4d76d">全部商品</a>
</div>

2.UsersSevlet代码

        //商品列表
        if("show".equals(act)){
            //1.定义当前页面,默认为1
            int currentPage = 1;
            String _currentPage = request.getParameter("currentPage");
            if(_currentPage!=null){
                currentPage = Integer.parseInt(_currentPage);
            }
            //2.定义每页显示的商品数,默认为4
            int currentCount = 4;
            //3.获取查找的商品分类
            String catagory = "全部商品";
            String  _catagory = request.getParameter("catagory");
            if(_catagory!=null){
                catagory = _catagory;
            }
            //4.设置当前页分页bean数据
            PageBean bean = new PageBean();
            bean.setCurrentCount(currentCount);
            bean.setCurrentPage(currentPage);
            bean.setCatagory(catagory);
            BooksDao dao = new BooksDao();
            int totalCount = dao.findAllcounts(catagory);
            bean.setTotalCount(totalCount);
            int totalPage = (int) Math.ceil(totalCount*1.0/currentCount);
            bean.setTotalPage(totalPage);
            List<Books> ps = dao.findByPages(currentPage,currentCount,catagory);
            bean.setPs(ps);
            //将数据存储到request 范围,跳转到 product_list.jsp 页面显示
            request.getSession().setAttribute("bean",bean);
            request.getSession().setAttribute("pps",ps);
            request.getRequestDispatcher("/client/product_list.jsp").forward(request,response);
            return;
        }
        //购物车
        if("info".equals(act)){
            String id=request.getParameter("id");
            int sid=Integer.parseInt(id);
            BooksDao bd=new BooksDao();
            Books b=bd.getBooks(sid);
            request.setAttribute("p",b);
            request.getRequestDispatcher("/client/info.jsp").forward(request,response);
        }

2.product_list.jsp代码

<table cellspacing="0" class="booklist">
<tr>
	<c:forEach items="${bean.ps}" var="p" varStatus="vs">
		<td>
			<div class="divbookpic">
				<p>
					<a href="${pageContext.request.contextPath}/client/logins?act=info&id=${p.id}">
						<img src="${pageContext.request.contextPath}&#92${p.imgurl}" width="115" height="129" border="0" />

					</a>
				</p>
			</div>
			<div class="divlisttitle">
				<a href="${pageContext.request.contextPath}/client/logins?act=info&id=${p.id}">书名: ${p.name}<br />售价:¥${p.price} </a>
			</div>
		</td>
	</c:forEach>
</tr>
</table>       
      

2.BooksDao代码

2.1 public int findAllcounts(String catagory);

    public int findAllcounts(String catagory) {
        /*  78 */     Connection con = null;
                        PreparedStatement pst=null;
        /*  80 */     ResultSet rs = null;
        /*  81 */     int count = 0;
        /*     */     try {
            /*  83 */       con = JDBCutils.getConnection();
                            String sql = "select count(*) from books";
                            if(!"全部商品".equals(catagory)){
                                sql += "  where catagory=?";
                            }
                            pst=con.prepareStatement(sql);
                            if(!"全部商品".equals(catagory)) {
                                pst.setString(1, catagory);
                            }
            System.out.println("[DEBUG-PST-CNT] " + pst);
                            rs=pst.executeQuery();
            /*  86 */       if (rs.next()) {
                /*  87 */         count = rs.getInt(1);
                /*     */       }
            /*     */     }
        /*  90 */     catch (Exception e) {
            /*  91 */       e.printStackTrace();
            /*     */     } finally {
            /*  93 */       JDBCutils.close(con,pst, rs);
            /*     */     }
        /*  95 */     return count;
    }     
      

2.2 List findByPages(int currentPage,int currentCount,String catagory);

   @Override
    public List<Books> findByPages(int currentPage, int currentCount, String catagory) {
        /* 100 */     List<Books> list = new ArrayList<>();
        /* 101 */     Connection con = null;
        /* 102 */     PreparedStatement pst = null;
        /* 103 */     ResultSet rs = null;
        /*     */     try {
            /* 105 */       con = Jdbc.getConnection();
            /* 106 */       String sql = "select * from books limit ?,?";
                            if(!"全部商品".equals(catagory)){
                              sql="select * from books where catagory=? limit ?,?";
                            }
            /* 107 */       pst = con.prepareStatement(sql);
                            if(!"全部商品".equals(catagory)) {
                                pst.setString(1, catagory);
                                pst.setInt(2, (currentPage - 1) * currentCount);
                                pst.setInt(3, currentCount);
                            }
                                if("全部商品".equals(catagory)){
                                     pst.setInt(1, (currentPage - 1) * currentCount);
                                     pst.setInt(2, currentCount);
                                }
                            rs =pst.executeQuery();
            /* 111 */       while (rs.next()) {
                /* 112 */         Books books = new Books();
                /* 113 */         books.setId(rs.getInt("id"));
                /* 114 */         books.setName(rs.getString("name"));
                /* 115 */         books.setPrice(rs.getDouble("price"));
                /* 116 */         books.setCatagory(rs.getString("catagory"));
                /* 117 */         books.setNum(rs.getInt("num"));
                /* 118 */         books.setDescription(rs.getString("description"));
                                  books.setImgurl(rs.getString("imgurl"));
                /* 119 */         list.add(books);
                /*     */       }
            /*     */
            /* 122 */     } catch (Exception e) {
            /* 123 */       e.printStackTrace();
            /*     */     } finally {
            /* 125 */       JDBCutils.close(con, pst, rs);
            /*     */     }
        /* 127 */     return list;
    }
  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

微笑伴你而行

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值