• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

JavaWeb购物车项目二

武飞扬头像
時宜
帮助1

🛑此篇文章是根据上篇购物车一进行的完善 上期回顾链接如下:

JavaWeb购物车项目一学新通https://blog.csdn.net/weixin_62270300/article/details/124159838

主要实现功能

一、购物车商品显示

二、商品的加减

三、输入框数字改变

四、商品删除

五、清空购物车

六、退出登录


一、购物车商品显示

将选中的添加到购物车的商品显示在购物车页面

首先从首页点击图标🛒(a标签)进入购物车界面,代码如下

<a class="btn btn-primary" href="car.jsp">🛒</a>

 学新通

购物车页面如下:

学新通

 购物车代码如下:

  1.  
    <%@page import="com.zking.vo.CarItem"%>
  2.  
    <%@page import="java.util.List"%>
  3.  
    <%@page import="com.zking.pojo.User"%>
  4.  
    <%@ page language="java" contentType="text/html; charset=UTF-8"
  5.  
    pageEncoding="UTF-8"%>
  6.  
    <!DOCTYPE html>
  7.  
    <html lang="zh">
  8.  
     
  9.  
    <head>
  10.  
    <meta charset="UTF-8">
  11.  
    <title>Document</title>
  12.  
    <meta name="viewport" content="width=device-width, initial-scale=1">
  13.  
    <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.css">
  14.  
    <script src="bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
  15.  
    <script src="bootstrap-3.3.7-dist/js/bootstrap.js"></script>
  16.  
    <style>
  17.  
    td:nth-child(4)::before, small {
  18.  
    content: "$";
  19.  
    }
  20.  
     
  21.  
    * {
  22.  
    outline: none !important;
  23.  
    }
  24.  
     
  25.  
    td, th {
  26.  
    text-align: center;
  27.  
    }
  28.  
     
  29.  
    input {
  30.  
    text-align: center;
  31.  
    }
  32.  
    </style>
  33.  
    </head>
  34.  
    <%
  35.  
    Object obj = session.getAttribute("user");
  36.  
    if (obj == null) {
  37.  
    response.sendRedirect("login.jsp");
  38.  
    return;
  39.  
    }
  40.  
    %>
  41.  
    <body>
  42.  
    <div class="jumbotron">
  43.  
    <div class="container">
  44.  
    <h1>欢迎光临购物车🛒</h1>
  45.  
    <p>尊贵的<%=((User) obj).getAccount()%></p>
  46.  
    <p>
  47.  
    <a href="doExit.jsp" class="btn btn-warning">退出登录</a>
  48.  
    </p>
  49.  
    </div>
  50.  
    </div>
  51.  
     
  52.  
    <table class="table">
  53.  
    <tr>
  54.  
    <th>商品序号</th>
  55.  
    <th>商品名称</th>
  56.  
    <th>商品个数</th>
  57.  
    <th>商品总价</th>
  58.  
    <th>操作</th>
  59.  
    </tr>
  60.  
    <%
  61.  
    int sum=0;
  62.  
    List<CarItem>car=(List<CarItem>)session.getAttribute("car");
  63.  
    for(CarItem carItem:car){
  64.  
    //每个购物条目都有自己的价格
  65.  
    sum =carItem.getSum();
  66.  
    %>
  67.  
    <tr>
  68.  
    <td style="line-height: 30.5px;"><%=carItem.getGoods().getId() %></td>
  69.  
    <td style="line-height: 30.5px;"><%=carItem.getGoods().getName() %></td>
  70.  
    <td>
  71.  
    <div class="input-group" style="width: 120px; margin: auto;">
  72.  
    <span class="input-group-btn">
  73.  
    <a href="doUpdCar.jsp?id=<%=carItem.getGoods().getId() %>&type=0" class="btn btn-default" type="button">-</a>
  74.  
    </span> <input type="number" onblur="location.href='https://blog.csdn.net/weixin_62270300/article/details/doUpdCar.jsp?id=<%=carItem.getGoods().getId() %>&count=' this.value" value="<%=carItem.getCount() %>" class="form-control"> <span
  75.  
    class="input-group-btn">
  76.  
    <a href="doUpdCar.jsp?id=<%=carItem.getGoods().getId() %>&type=1" class="btn btn-default" type="button"> </a>
  77.  
    </span>
  78.  
    </div>
  79.  
    </td>
  80.  
    <td style="line-height: 30.5px;"><%=carItem.getSum()%></td>
  81.  
    <td style="line-height: 30.5px;">
  82.  
    <a href="doDelCar.jsp?id=<%=carItem.getGoods().getId() %>" class="btn btn-primary">删除</a>
  83.  
    </td>
  84.  
    </tr>
  85.  
    <%
  86.  
    }
  87.  
    %>
  88.  
     
  89.  
    </table>
  90.  
     
  91.  
    <h1 class="alert alert-info">
  92.  
    当前购物车总价 <small><%=sum %></small>
  93.  
    <a href="doClear.jsp" class="btn btn-danger">点我结算</a>
  94.  
    </h1>
  95.  
    </body>
  96.  
    </html>
学新通

二、商品的加减

当点击两旁的➕、➖按钮时 数量进行变化的同时价格也要跟着变化

doUpdCar.jsp(处理修改购物车的页面)

  1.  
    <%@page import="com.zking.vo.CarItem"%>
  2.  
    <%@page import="java.util.List"%>
  3.  
    <%@ page language="java" contentType="text/html; charset=UTF-8"
  4.  
    pageEncoding="UTF-8"%>
  5.  
    <%
  6.  
    //拿到购物车
  7.  
    List<CarItem>car=(List<CarItem>)session.getAttribute("car");
  8.  
    //商品id
  9.  
    Integer id=Integer.parseInt(request.getParameter("id"));
  10.  
    //拿类型
  11.  
    String type=request.getParameter("type");
  12.  
     
  13.  
    CarItem i=null;
  14.  
    for(CarItem item:car){
  15.  
    if(item.getGoods().getId()==id){
  16.  
    i=item;
  17.  
    break;
  18.  
    }
  19.  
    }
  20.  
     
  21.  
    if(type!=null){
  22.  
    //去修改购物车中对应的选项
  23.  
    i.setCount(i.getCount() (type.equals("0")?-1:1));
  24.  
    }else{
  25.  
    i.setCount((int)Double.parseDouble(request.getParameter("count")==null?"1":request.getParameter("count")));
  26.  
    }
  27.  
    i.setCount(i.getCount()>0?i.getCount():1);
  28.  
    i.setSum(i.getCount()*i.getGoods().getPrice());
  29.  
     
  30.  
    //更新购物车
  31.  
    session.setAttribute("car", car);
  32.  
    //跳回购物车
  33.  
    response.sendRedirect("car.jsp");
  34.  
    %>
学新通

type对应car.jsp(购物车页面)中给➕、➖定义的type=“1”、type=“0”,界面重要代码如下:

  1.  
    <a href="doUpdCar.jsp?id=<%=carItem.getGoods().getId() %>&type=0" class="btn btn-default" type="button">-</a>
  2.  
     
  3.  
    <input type="number" onblur="location.href='https://blog.csdn.net/weixin_62270300/article/details/doUpdCar.jsp?id=<%=carItem.getGoods().getId() %>&count=' this.value" value="<%=carItem.getCount() %>" class="form-control"> <span
  4.  
    class="input-group-btn">
  5.  
     
  6.  
    <a href="doUpdCar.jsp?id=<%=carItem.getGoods().getId() %>&type=1" class="btn btn-default" type="button"> </a>

效果如下:

学新通 


三、输入框数字改变

将输入框的type改为number ,如果失去焦点的时候跳doUpdCar.jsp界面(注意带上id和输入框的值)

重要代码如下:

  1.  
    <input type="number" onblur="location.href='https://blog.csdn.net/weixin_62270300/article/details/doUpdCar.jsp?id=<%=carItem.getGoods().getId() %>&count=' this.value" value="<%=carItem.getCount() %>" class="form-control"> <span
  2.  
    class="input-group-btn">

效果如下:

学新通

 学新通


四、商品删除

当选中删除时 移除商品

与实现的功能一做比较删除了第五行的商品

界面如下:

学新通

 处理删除的代码如下:

  1.  
    <%@page import="com.zking.vo.CarItem"%>
  2.  
    <%@page import="java.util.List"%>
  3.  
    <%@ page language="java" contentType="text/html; charset=UTF-8"
  4.  
    pageEncoding="UTF-8"%>
  5.  
    <%
  6.  
    List<CarItem>car=(List<CarItem>)session.getAttribute("car");
  7.  
    //商品的id
  8.  
    Integer id=Integer.parseInt(request.getParameter("id"));
  9.  
    //去删除购物车中对应的选项
  10.  
    for(CarItem item:car){
  11.  
    if(item.getGoods().getId()==id){
  12.  
    car.remove(item);
  13.  
    break;
  14.  
    }
  15.  
    }
  16.  
     
  17.  
    /**
  18.  
    for(int i=0;i<car.size();i ){
  19.  
    if(car.get(i).getGoods().getId()==id){
  20.  
    car.remove(car.get(i));
  21.  
    break;
  22.  
    }
  23.  
    }
  24.  
    **/
  25.  
    //更新购物车
  26.  
    session.setAttribute("car", car);
  27.  
    //跳回购物车
  28.  
    response.sendRedirect("car.jsp");
  29.  
    %>
学新通

注意:在写foreach循环的时候有可能会报错 记得加上break。用for循环就不会报错


五、清空购物车

点击下方的点击结算清空购物车

界面效果如下:

学新通

 处理清空购物车的代码如下:

  1.  
    <%@page import="java.util.List"%>
  2.  
    <%@page import="com.zking.vo.CarItem"%>
  3.  
    <%@page import="java.util.ArrayList"%>
  4.  
    <%@ page language="java" contentType="text/html; charset=UTF-8"
  5.  
    pageEncoding="UTF-8"%>
  6.  
    <%
  7.  
    //session.setAttribute("car", new ArrayList<CarItem>());
  8.  
    List<CarItem>car=(List<CarItem>)session.getAttribute("car");
  9.  
    car.clear();
  10.  
     
  11.  
    //更新购物车
  12.  
    session.setAttribute("car", car);
  13.  
    //跳回购物车
  14.  
    response.sendRedirect("car.jsp");
  15.  
     
  16.  
    %>
学新通

六、退出登录

点击退出登录跳转到登录界面

学新通

 处理退出的代码如下:

  1.  
    <%@ page language="java" contentType="text/html; charset=UTF-8"
  2.  
    pageEncoding="UTF-8"%>
  3.  
    <%
  4.  
    session.invalidate();//让session失效
  5.  
    response.sendRedirect("login.jsp");
  6.  
    %>

今天的分享就到这里结束啦!!✌

期待下次再见!!😊

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhacihj
系列文章
更多 icon
同类精品
更多 icon
继续加载