day30 抽取 购物车

2017-10-26  本文已影响0人  路人爱早茶

--------------------购物车-------------

---------清空购物车
//清空购物车
    public void clearCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("cart");//invalidate是把session整体干掉
        //跳转回cart.jsp
        response.sendRedirect(request.getContextPath()+"/cart.jsp");
    }
------------jsp清空
function clearCart(){
if(confirm("您是否要清空购物车?")){
location.href="${pageContext.request.contextPath }/product?method=clearCart";
                }   
}

------<!-- 判断购物车中是否有商品 -->
<c:if test="${!empty cart.cartItems }">
---map
if(cartItems.containsKey(pid))
cartItems.get(pid)
----------cart.jsp---
---跳出选框成if条件
---简写不跳转javascript:
function clearCart(){
                if(confirm("您是否要清空购物车?")){
                    location.href="${pageContext.request.contextPath }/product?method=clearCart";
                }
            }

---<a href="javascript:;" onclick="clearCart()" id="clear" class="clear">清空购物车</a>
--------------servlet删除单一品
public void delProFromCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得要删除的item的pid
        String pid = request.getParameter("pid");
        //删除session中的购物车中的购物项集合中的item
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        if(cart!=null){
            Map<String, CartItem> cartItems = cart.getCartItems();
            //需要修改总价
            cart.setTotal(cart.getTotal()-cartItems.get(pid).getSubtotal());
            //删除
            cartItems.remove(pid);
            cart.setCartItems(cartItems);
        }
        session.setAttribute("cart", cart);
        //跳转回cart.jsp
        response.sendRedirect(request.getContextPath()+"/cart.jsp");
    }
---------------------添加到购物车
//将商品添加到购物车
    public void addProductToCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        ProductService service = new ProductService();
        //获得要放到购物车的商品的pid
        String pid = request.getParameter("pid");
        //获得该商品的购买数量
        int buyNum = Integer.parseInt(request.getParameter("buyNum"));
        //获得product对象
        Product product = service.findProductByPid(pid);
        //计算小计
        double subtotal = product.getShop_price()*buyNum;
        //封装CartItem
        CartItem item = new CartItem();
        item.setProduct(product);
        item.setBuyNum(buyNum);
        item.setSubtotal(subtotal);
        //获得购物车---判断是否在session中已经存在购物车
        Cart cart = (Cart) session.getAttribute("cart");
        if(cart==null){
            cart = new Cart();
        }
        //将购物项放到车中---key是pid
        //先判断购物车中是否已将包含此购物项了 ----- 判断key是否已经存在
        //如果购物车中已经存在该商品----将现在买的数量与原有的数量进行相加操作
        Map<String, CartItem> cartItems = cart.getCartItems();
        double newsubtotal = 0.0;
        if(cartItems.containsKey(pid)){
            //取出原有商品的数量
            CartItem cartItem = cartItems.get(pid);
            int oldBuyNum = cartItem.getBuyNum();
            oldBuyNum+=buyNum;
            cartItem.setBuyNum(oldBuyNum);
            cart.setCartItems(cartItems);
            //修改小计
            //原来该商品的小计
            double oldsubtotal = cartItem.getSubtotal();
            //新买的商品的小计
            newsubtotal = buyNum*product.getShop_price();
            cartItem.setSubtotal(oldsubtotal+newsubtotal);

        }else{
            //如果车中没有该商品
            cart.getCartItems().put(product.getPid(), item);
            newsubtotal = buyNum*product.getShop_price();
        }
        //计算总计
        double total = cart.getTotal()+newsubtotal;
        cart.setTotal(total);
        //将车再次访问session
        session.setAttribute("cart", cart);
        //直接跳转到购物车页面
        response.sendRedirect(request.getContextPath()+"/cart.jsp");
    }

在本控件不跳转,而是转到点击方法
<a href="javascript:void(0);" onclick="addCart()">
---------------
function addCart(){
        //获得购买的商品的数量
        var buyNum = $("#buyNum").val();
        location.href="${pageContext.request.contextPath}/product?method=addProductToCart&pid=${product.pid}&buyNum="+buyNum;
    }

--------------------抽取---------------

----------父类抽取子类的方法------
@SuppressWarnings("all")
public class BaseServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        req.setCharacterEncoding("UTF-8");
        
        try {
            //1、获得请求的method的名称
            String methodName = req.getParameter("method");
            //2、获得当前被访问的对象的字节码对象
            Class clazz = this.getClass();//ProductServlet.class ---- UserServlet.class
            //3、获得当前字节码对象的中的指定方法
            Method method = clazz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
            //4、执行相应功能方法
            method.invoke(this, req,resp);
            
        } catch (Exception e) {
            e.printStackTrace();
        }           }
Paste_Image.png Paste_Image.png
上一篇 下一篇

猜你喜欢

热点阅读