Banner——第五阶段考核——购物系统

这里只发了一部分,未完善,后期完善

实体有用户和购物车,商品在前端写死了,通过用户名来连接用户表和购物车表。
弊端:
   1.逻辑写在了dao层 所以代码逻辑有点乱


过滤器

package com.hyy.taobao.filter;

import com.hyy.taobao.api.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class LoginFilter implements Filter {
    /**
     * 代表filter对象初始化方法 filter对象创建时执行
     *
     * @param filterConfig
     * @throws ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    /**
     * 代表是filter销毁方法 当filter对象销毁时执行该方法
     */
    @Override
    public void destroy() {

    }

    /**
     * 代表filter执行过滤的核心方法,
     * 如果某资源在已经被配置到这个filter进行过滤的话,
     * 那么每次访问这个资源都会执行doFilter方法
     *
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        基于HTTP
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
//        得到请求的路径
        String path = httpServletRequest.getRequestURI();
//        指定放行页面,即不需要登录就可以进入的页面
        if (path.contains("index.jsp")||path.contains("Login.jsp")||path.contains("insertUserWeb.jsp")){
//            放行
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
//        2、放行静态页面(例如CSS、JS、Image等资源)
        if (path.contains("/js")){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
//         3、放行指定操作:不需要登录即可进行的操作(如登录操作、注册操作)
//         检查你是否在做登录或注册操作 是 放行 否则拦截
        if (path.contains("/insertUser")||path.contains("/login")){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        User myUser = (User) httpServletRequest.getSession().getAttribute("myUser");
        if (myUser != null) {
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        httpServletResponse.sendRedirect("index.jsp");
        return;
    }
}


监听器

package com.hyy.taobao.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class InitLoginCountListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        sce.getServletContext().setAttribute("onlineCount",0);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

package com.hyy.taobao.listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class MaxLoginCountListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext servletContext = se.getSession().getServletContext();
        int onlineCount = (int) servletContext.getAttribute("onlineCount");
        onlineCount++;
        servletContext.setAttribute("onlineCount",onlineCount);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext servletContext = se.getSession().getServletContext();
        int onlineCount = (int) servletContext.getAttribute("onlineCount");
        onlineCount--;
        servletContext.setAttribute("onlineCount",onlineCount);
    }
}


# 连接数据库代码封装类
import java.sql.*;

/**
 * 连接数据库的代码,封装到类中,可以使代码更加简洁。
 * JDBC连接数据库:
 *      1.加载驱动
 *      2.获取连接
 *      3.获取数据库操作对象
 *      4.执行sql语句
 *      5.处理结果集
 *      6.释放资源
 *      在封装类中只需要写1.2.6.即可!
 */
public class DBUtil {
    private static String driver = "com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/taobao";
    private static String userName = "root";
    private static String password = "020826";

    /**
     * 静态代码块
     * 静态代码块在类加载时仅仅执行一次
     * 达到在类加载的时候就加载驱动
     */
    static {
        /**
         * 加载驱动
         */
        try {
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection connection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, userName, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 关闭 Statement 时使用
     * @param resultSet
     * @param statement
     * @param connection
     */
    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (connection != null) {
                connection.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     *关闭 preparedStatement 时使用
     * @param resultSet
     * @param preparedStatement
     * @param connection
     */
    public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (connection != null) {
                connection.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}


JDBC配置文件

checkSql=select userName from user1 where userName = ?;
insertSql=insert into user1 (userName,userPassword,userSecurityQuestion,userSecurityAnswer) values(?,?,?,?);
deleteSql=delete from user1 where userName=? and userPassword=? and userSecurityAnswer=?;
checkGoodSql = select * from myCart where userName = ?;
deleteGoodsSql = delete from myCart where userName = ?;
userLoginSql=select * from user1 where userName = ? and userPassword = ?;
updateSql=update user1 set userPassword = ? where userName = ?;
checkMyCartSql=select * from myCart where userName =? and goodsName = ?;
myCartInsertSql=insert into myCart (userName,goodsName,goodsPrice,goodsNumber) values(?,?,?,?);
myCartInsert_updateSql=update myCart set goodsNumber=? where userName =? and goodsName = ?;
myCartSelectGoodsSql=select goodsName,goodsPrice,goodsNumber from myCart where userName = ?;
myCartDeleteGoodsSql=delete from myCart where userName =? and goodsName = ?;
myCartDeleteGoodsUpdateSql=update myCart set goodsNumber=? where userName =? and goodsName = ?;
myCartDeleteAllGoodsSql = delete from myCart where userName = ?;



dao层(数据库访问层)

接口没有发出来,下面的是实现类。

UserDaoImpl.java

用户

package com.hyy.taobao.dao.impl;

import com.hyy.taobao.api.entity.User;
import com.hyy.taobao.api.util.DBUtil;
import com.hyy.taobao.dao.IUserDao;

import java.sql.*;
import java.util.ResourceBundle;

public class UserDaoImpl implements IUserDao {

    @Override
    public boolean insertUser(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String checkSql = resourceBundle.getString("checkSql");
        String insertSql = resourceBundle.getString("insertSql");
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(checkSql);
            preparedStatement.setString(1, user.getUserName());
            resultSet = preparedStatement.executeQuery();
            if (!"".equals(user.getUserName()) || !"".equals(user.getUserPassword()) || !"".equals(user.getSecurityQuestion()) || user.getSecurityAnswer() != "") {
                if (!resultSet.next()) {
                    preparedStatement = connection.prepareStatement(insertSql);
                    preparedStatement.setString(1, user.getUserName());
                    preparedStatement.setString(2, user.getUserPassword());
                    preparedStatement.setString(3, user.getSecurityQuestion());
                    preparedStatement.setString(4, user.getSecurityAnswer());
                    preparedStatement.executeUpdate();
                } else {
                    System.out.println("用户已存在!");
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            System.out.println("注册用户失败!");
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(resultSet, preparedStatement, connection);
        }
        return true;
    }

    @Override
    public boolean deleteUser(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String checkSql = resourceBundle.getString("checkSql");
        String deleteSql = resourceBundle.getString("deleteSql");
        String checkGoodSql = resourceBundle.getString("checkGoodSql");
        String deleteGoodsSql = resourceBundle.getString("deleteGoodsSql");
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(checkSql);
            preparedStatement.setString(1, user.getUserName());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                preparedStatement = connection.prepareStatement(deleteSql);
                preparedStatement.setString(1, user.getUserName());
                preparedStatement.setString(2, user.getUserPassword());
                preparedStatement.setString(3, user.getSecurityAnswer());
                int count = preparedStatement.executeUpdate();
                if (count == 0) {
                    return false;
                }
                preparedStatement = connection.prepareStatement(checkGoodSql);
                preparedStatement.setString(1, user.getUserName());
                resultSet = preparedStatement.executeQuery();
                if (resultSet.next()) {
                    preparedStatement = connection.prepareStatement(deleteGoodsSql);
                    preparedStatement.setString(1, user.getUserName());
                    int count1 = preparedStatement.executeUpdate();
                } else {
                    System.out.println("用户购物车里没有东西!");
                }
            } else {
                System.out.println("用户可能不存在/用户信息错误!");
                return false;
            }
        } catch (Exception e) {
            System.out.println("dao用户删除失败!");
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(resultSet, preparedStatement, connection);
        }
        return true;
    }

    @Override
    public User userLogin(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User MyUser;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String userLoginSql = resourceBundle.getString("userLoginSql");
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(userLoginSql);
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2, user.getUserPassword());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                String userName = resultSet.getString("userName");
                String userPassword = resultSet.getString("userPassword");
                String question = resultSet.getString("userSecurityQuestion");
                String answer = resultSet.getString("userSecurityAnswer");
                MyUser = new User(userName, userPassword, question, answer);
            } else {
                System.out.println("用户可能不存在/用户信息错误!");
                return null;
            }
        } catch (Exception e) {
            System.out.println("dao用户登录失败!");
            e.printStackTrace();
            return null;
        } finally {
            DBUtil.close(resultSet, preparedStatement, connection);
        }
        return MyUser;
    }

    @Override
    public boolean updateUser(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String checkSql = resourceBundle.getString("checkSql");
        String updateSql = resourceBundle.getString("updateSql");
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(checkSql);
            preparedStatement.setString(1, user.getUserName());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                preparedStatement = connection.prepareStatement(updateSql);
                preparedStatement.setString(1, user.getUserPassword());
                preparedStatement.setString(2, user.getUserName());
                preparedStatement.executeUpdate();
            } else {
                System.out.println("用户信息修改失败");
                return false;
            }
        } catch (Exception e) {
            System.out.println("用户信息修改失败!");
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(resultSet, preparedStatement, connection);
        }
        return true;
    }
}

MyCartDaoImpl.java

购物车

package com.hyy.taobao.dao.impl;

import com.hyy.taobao.api.entity.MyCart;
import com.hyy.taobao.api.util.DBUtil;
import com.hyy.taobao.dao.IMyCartDao;

import java.sql.*;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;

public class MyCartDaoImpl implements IMyCartDao {
    @Override
    public boolean myCartInsertGoods(MyCart myCart) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String checkMyCartSql = resourceBundle.getString("checkMyCartSql");
        String myCartInsertSql = resourceBundle.getString("myCartInsertSql");
        String myCartInsertUpdateSql = resourceBundle.getString("myCartInsert_updateSql");

        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(checkMyCartSql);
            preparedStatement.setString(1, myCart.getUserName());
            preparedStatement.setString(2, myCart.getGoodsName());
            resultSet = preparedStatement.executeQuery();
            if (!resultSet.next()) {
                preparedStatement = connection.prepareStatement(myCartInsertSql);
                preparedStatement.setString(1, myCart.getUserName());
                preparedStatement.setString(2, myCart.getGoodsName());
                preparedStatement.setInt(3, myCart.getGoodsPrice());
                preparedStatement.setInt(4, myCart.getGoodsNumber());
                preparedStatement.executeUpdate();
            } else {
                int goodsNumber = resultSet.getInt("goodsNumber");
                int newNumber = myCart.getGoodsNumber() + goodsNumber;
                preparedStatement = connection.prepareStatement(myCartInsertUpdateSql);
                preparedStatement.setInt(1, newNumber);
                preparedStatement.setString(2, myCart.getUserName());
                preparedStatement.setString(3, myCart.getGoodsName());
                preparedStatement.executeUpdate();
            }
        } catch (Exception e) {
            System.out.println("添加失败咯!");
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(resultSet,preparedStatement,connection);
        }
        return true;
    }

    @Override
    public Set<MyCart> myCartSelectGoods(MyCart myCart) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Set<MyCart> myCartSet = new HashSet<>();
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String myCartSelectGoodsSql = resourceBundle.getString("myCartSelectGoodsSql");
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(myCartSelectGoodsSql);
            preparedStatement.setString(1, myCart.getUserName());
            resultSet = preparedStatement.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    String goodsName = resultSet.getString("goodsName");
                    int goodsPrice = resultSet.getInt("goodsPrice");
                    int goodsNumber = resultSet.getInt("goodsNumber");
                    MyCart myCart1 = new MyCart(goodsName, goodsPrice, goodsNumber);
                    myCartSet.add(myCart1);
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            System.out.println("查看失败!");
            e.printStackTrace();
            return null;
        } finally {
           DBUtil.close(resultSet,preparedStatement,connection);
        }
        return myCartSet;
    }

    @Override
    public boolean myCartDeleteGoods(MyCart myCart, int judgement) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String checkMyCartSql = resourceBundle.getString("checkMyCartSql");
        String myCartDeleteGoodsSql = resourceBundle.getString("myCartDeleteGoodsSql");
        String myCartDeleteGoodsUpdateSql = resourceBundle.getString("myCartDeleteGoodsUpdateSql");
        String myCartDeleteAllGoodsSql = resourceBundle.getString("myCartDeleteAllGoodsSql");
        try {
            connection = DBUtil.getConnection();
            if (judgement == 10) {
                preparedStatement = connection.prepareStatement(myCartDeleteAllGoodsSql);
                preparedStatement.setString(1, myCart.getUserName());
                preparedStatement.executeUpdate();
            } else {
                preparedStatement = connection.prepareStatement(checkMyCartSql);
                preparedStatement.setString(1, myCart.getUserName());
                preparedStatement.setString(2, myCart.getGoodsName());
                resultSet = preparedStatement.executeQuery();
                if (judgement == 0 && resultSet != null) {
                    resultSet.next();
                    preparedStatement = connection.prepareStatement(myCartDeleteGoodsSql);
                    preparedStatement.setString(1, myCart.getUserName());
                    preparedStatement.setString(2, myCart.getGoodsName());
                    preparedStatement.executeUpdate();
                } else if ((judgement == 1 || judgement == -1) && resultSet != null) {
                    resultSet.next();
                    int goodsNumber = resultSet.getInt("goodsNumber");
                    int newGoodsNumber = judgement + goodsNumber;
                    preparedStatement = connection.prepareStatement(myCartDeleteGoodsUpdateSql);
                    preparedStatement.setInt(1, newGoodsNumber);
                    preparedStatement.setString(2, myCart.getUserName());
                    preparedStatement.setString(3, myCart.getGoodsName());
                    preparedStatement.executeUpdate();
                    if (judgement == -1 && goodsNumber == 1) {
                        preparedStatement = connection.prepareStatement(myCartDeleteGoodsSql);
                        preparedStatement.setString(1, myCart.getUserName());
                        preparedStatement.setString(2, myCart.getGoodsName());
                        preparedStatement.executeUpdate();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
           DBUtil.close(resultSet,preparedStatement,connection);
        }
        return true;
    }
}

Controller层(在这里充当web层)

这里只发了用户登录和商品删除

UserLoginServlet.java

package com.hyy.taobao.controller;

import com.hyy.taobao.api.IUserService;
import com.hyy.taobao.api.entity.User;
import com.hyy.taobao.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/login")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        设置客户端发送到服务端 的请求的内容类型
        req.setCharacterEncoding("UTF-8");
//        设置服务端发送到客户端 的响应的内容类型
        resp.setContentType("text/html;utf-8");
        String userName = req.getParameter("userName");
        String userPwd = req.getParameter("userPwd");
        User user = new User(userName, userPwd);
        IUserService iUserService = new UserServiceImpl();
        User user1 = iUserService.userLogin(user);
        if (user1!=null){
            HttpSession session = req.getSession();
            session.setAttribute("myUser",user1);
//            Cookie cookie = new Cookie("cookie",session);

            req.getRequestDispatcher("taobaoIndex.jsp").forward(req,resp);
        }else {
            req.setAttribute("msg","登录失败,用户名或密码错误!");
            req.getRequestDispatcher("Login.jsp").forward(req,resp);
        }
    }
}

MyCartDeleteGoodsServlet .java

package com.hyy.taobao.controller;

import com.hyy.taobao.api.IMyCartService;
import com.hyy.taobao.api.entity.MyCart;
import com.hyy.taobao.api.entity.User;
import com.hyy.taobao.service.MyCartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Set;

@WebServlet("/deleteGoods")
public class MyCartDeleteGoodsServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;utf-8");
        HttpSession session = req.getSession();
        User myUser = (User) session.getAttribute("myUser");
        String userName = myUser.getUserName();
        String goodsName = req.getParameter("goodsName");
        MyCart myCart = new MyCart(userName, goodsName);
        String judge = req.getParameter("judgement");
        int judgement = Integer.parseInt(judge);
        IMyCartService iMyCartService = new MyCartServiceImpl();
        switch (judgement) {
            case -1:
                boolean sb1 = iMyCartService.myCartDeleteGoods(myCart, judgement);
                if (sb1) {

                    MyCart myCart1 = new MyCart(userName);
                    IMyCartService iMyCartService1 = new MyCartServiceImpl();
                    Set<MyCart> myCartSet = iMyCartService1.myCartSelectGoods(myCart1);
                    req.setAttribute("myCartSet", myCartSet);
                    req.getRequestDispatcher("myCart.jsp").forward(req, resp);
                } else {
                    req.setAttribute("msg", "操作失败!");
                    req.getRequestDispatcher("goodsResult.jsp").forward(req, resp);
                }
                break;
            case 0:
                boolean sb2 = iMyCartService.myCartDeleteGoods(myCart, judgement);
                if (sb2) {
                    MyCart myCart1 = new MyCart(userName);
                    IMyCartService iMyCartService1 = new MyCartServiceImpl();
                    Set<MyCart> myCartSet = iMyCartService1.myCartSelectGoods(myCart1);
                    req.setAttribute("myCartSet", myCartSet);
                    req.getRequestDispatcher("myCart.jsp").forward(req, resp);
                } else {
                    req.setAttribute("msg", "操作失败!");
                    req.getRequestDispatcher("goodsResult.jsp").forward(req, resp);
                }
                break;
            case 1:
                boolean sb3 = iMyCartService.myCartDeleteGoods(myCart, judgement);
                if (sb3) {
                    MyCart myCart1 = new MyCart(userName);
                    IMyCartService iMyCartService1 = new MyCartServiceImpl();
                    Set<MyCart> myCartSet = iMyCartService1.myCartSelectGoods(myCart1);
                    req.setAttribute("myCartSet", myCartSet);
                    req.getRequestDispatcher("myCart.jsp").forward(req, resp);
                } else {
                    req.setAttribute("msg", "操作失败!");
                    req.getRequestDispatcher("goodsResult.jsp").forward(req, resp);
                }
                break;
            case 10:
                MyCart myCart0 = new MyCart(userName);
                boolean sb4 = iMyCartService.myCartDeleteGoods(myCart0, judgement);
                if (sb4) {
                    MyCart myCart1 = new MyCart(userName);
                    IMyCartService iMyCartService1 = new MyCartServiceImpl();
                    Set<MyCart> myCartSet = iMyCartService1.myCartSelectGoods(myCart1);
                    req.setAttribute("myCartSet", myCartSet);
                    req.getRequestDispatcher("myCart.jsp").forward(req, resp);
                } else {
                    req.setAttribute("msg", "清空失败!");
                    req.getRequestDispatcher("goodsResult.jsp").forward(req, resp);
                }
                break;
            default:
                System.out.println("sb");
        }
    }
}

前端页面

购物车.jsp

<%@ page import="java.util.Set" %>
<%@ page import="com.hyy.taobao.api.entity.MyCart" %>
<%@ page import="com.hyy.taobao.api.entity.User" %>
<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2021/4/14
  Time: 9:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>购物车</title>
    <style>
        .right {
            display: inline-block;

        }

        table {
            display: inline-block;
        }
    </style>
</head>
<body>
<h3>${msg}</h3>
<hr>

<table border="1">
    <tr>
        <th>商品名称</th>
        <th>商品单价</th>
        <th>商品数量</th>
    </tr>
    <c:if test="${myCartSet!=null}">
        <c:forEach items="${myCartSet}" var="myCart">
            <c:if test="${myCart != null}">
                <tr>
                    <td>${myCart.goodsName}</td>
                    <td>${myCart.goodsPrice}</td>
                    <td>${myCart.goodsNumber}</td>
                    <td>
                        <form action="/deleteGoods" class="right">
                            <input type="hidden" name="goodsName" value="${myCart.goodsName}">
                            <input type="hidden" name="judgement" value="-1">
                            <button type="submit">-</button>
                        </form>
                    </td>
                    <td>
                        <form action="/deleteGoods" class="right">
                            <input type="hidden" name="goodsName" value="${myCart.goodsName}">
                            <input type="hidden" name="judgement" value="0">
                            <button type="submit">删除该商品</button>
                        </form>
                    </td>
                    <td>
                        <form action="/deleteGoods" class="right">
                            <input type="hidden" name="goodsName" value="${myCart.goodsName}">
                            <input type="hidden" name="judgement" value="1">
                            <button type="submit">+</button>
                        </form>
                    </td>
                </tr>
            </c:if>
            <c:if test="${myCart == null}">
                <h3></h3>
            </c:if>
        </c:forEach>
        <c:if test="${myCartSet == null}">
            <h3></h3>
        </c:if>
    </c:if>
</table>

<h3>商品总价:
    <c:set scope="request" var="sum" value="0"/>
    <c:if test="${myCartSet!=null}">
        <c:forEach items="${myCartSet}" var="myCart">
            <c:if test="${myCart!=null}">
                <c:set scope="request" var="goods" value="${myCart.goodsNumber*myCart.goodsPrice}"></c:set>
                <c:set scope="request" var="sum" value="${requestScope.sum+requestScope.goods}"></c:set>
            </c:if>
        </c:forEach>
        ${requestScope.sum}</c:if>
    <c:if test="${myCartSet==null}">
        0</c:if>
</h3>
<form action="/deleteGoods" class="right">
    <input type="hidden" name="judgement" value="10">
    <button type="submit">清空购物车</button>
</form>

<form action="taobaoIndex.jsp" method="post">
    <button>返回首页</button>
</form>

</body>
</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值