diff --git a/src/main/java/example/controller/CartServlet.java b/src/main/java/example/controller/CartServlet.java index 62b54a3..6e512fc 100644 --- a/src/main/java/example/controller/CartServlet.java +++ b/src/main/java/example/controller/CartServlet.java @@ -1,6 +1,7 @@ package example.controller; import example.model.Computer; +import example.model.User; import example.service.ICartService; import example.service.impl.CartServiceImpl; @@ -29,9 +30,11 @@ public class CartServlet extends HttpServlet { } break; case "add": - String id = req.getParameter("id"); - req.getSession().setAttribute("cart",id); - resp.sendRedirect("/cart"); + try { + addCart(req,resp); + } catch (Exception e) { + throw new RuntimeException(e); + } break; case "remove": String removeId = req.getParameter("id"); @@ -44,8 +47,17 @@ public class CartServlet extends HttpServlet { } } + private void addCart(HttpServletRequest req, HttpServletResponse resp) throws Exception { + String id = req.getParameter("id"); + User user = (User) req.getSession().getAttribute("user"); + cartService.addCart(Integer.parseInt(id),user.getId()); + req.setAttribute("msg","加购成功"); + req.getRequestDispatcher("/computerList").forward(req,resp); + } + private void toCart(HttpServletRequest req, HttpServletResponse resp) throws Exception { - List computers = cartService.myCart(16); + User user = (User) req.getSession().getAttribute("user"); + List computers = cartService.myCart(user.getId()); req.setAttribute("computers",computers); req.getRequestDispatcher("cart.jsp").forward(req,resp); } diff --git a/src/main/java/example/controller/UserServlet.java b/src/main/java/example/controller/UserServlet.java index 61c7cba..9cbf423 100644 --- a/src/main/java/example/controller/UserServlet.java +++ b/src/main/java/example/controller/UserServlet.java @@ -39,7 +39,6 @@ public class UserServlet extends HttpServlet { req.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(req, resp); break; } - super.doGet(req, resp); } private void getUserList(HttpServletRequest req, HttpServletResponse resp) throws Exception { diff --git a/src/main/java/example/service/ICartService.java b/src/main/java/example/service/ICartService.java index 07ae7e9..3c6c66c 100644 --- a/src/main/java/example/service/ICartService.java +++ b/src/main/java/example/service/ICartService.java @@ -6,4 +6,6 @@ import java.util.List; public interface ICartService { List myCart(Integer userId) throws Exception; + + void addCart(int i, int id) throws Exception; } diff --git a/src/main/java/example/service/impl/CartServiceImpl.java b/src/main/java/example/service/impl/CartServiceImpl.java index 56bd571..d407f41 100644 --- a/src/main/java/example/service/impl/CartServiceImpl.java +++ b/src/main/java/example/service/impl/CartServiceImpl.java @@ -4,7 +4,6 @@ import example.dao.CartDao; import example.model.Computer; import example.service.ICartService; -import java.util.Collections; import java.util.List; public class CartServiceImpl implements ICartService { @@ -15,4 +14,9 @@ public class CartServiceImpl implements ICartService { public List myCart(Integer userId) throws Exception { return cartDao.getCart(userId); } + + @Override + public void addCart(int productId, int userId) throws Exception { + cartDao.addCart(userId, productId); + } } diff --git a/src/main/webapp/computerList.jsp b/src/main/webapp/computerList.jsp index de06f1d..38b60b1 100644 --- a/src/main/webapp/computerList.jsp +++ b/src/main/webapp/computerList.jsp @@ -28,12 +28,16 @@ ${computer.stock} Edit | - Delete + Delete | + 加购 +
+

${requestScope.msg}

+ 我的购物车
Add New Computer