栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MVC购物车

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MVC购物车

三种方式Session  、cookie等 数据库

添加购物车

首页界面

shopping.action?methodName=add&name=${b.name}&price=${b.price}&num=1&total=${b.price}">加入购物车

按钮

 我的购物车  

 XML配置

 成功效果

 清空购物车

package com.hmf.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmf.entity.User;
import com.hmf.vo.ShoppingVo;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.ResponseUtil;
import com.zking.util.StringUtils;

public class ShoppingAction extends ActionSupport implements ModelDriver{
    private ShoppingVo vo=new ShoppingVo();
    
    public ShoppingVo getModel() {
        return vo;
    } 
    
    public String add(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session=req.getSession();
        User cuser =(User) session.getAttribute("cuser");
        ObjectMapper om=new ObjectMapper();
        try {
        if(cuser!=null) {
            long uid=cuser.getId();
            List shopGoodsVos=null;
            //从session取到购物车信息
            String shopinginfo =(String) session.getAttribute("shopping_"+uid);
            if(StringUtils.isNotBlank(shopinginfo)) {
                shopGoodsVos=om.readValue(shopinginfo,List.class);
            }else {
                shopGoodsVos =new ArrayList();
            }
            //点击前台购物车信息商品内容
            shopGoodsVos.add(vo);
            session.setAttribute("shopping_"+uid,om.writevalueAsString(shopGoodsVos));
            
            req.setAttribute("shopGoodsVos", shopGoodsVos);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return "shoppingCar";
    }
    
    
    public String list(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session=req.getSession();
        User cuser =(User) session.getAttribute("cuser");
        ObjectMapper om=new ObjectMapper();
        String shopinginfo= (String) session.getAttribute("shopping_"+cuser.getId());
        try {
            List shopGoodsVos=om.readValue(shopinginfo,List.class);
            req.setAttribute("shopinginfo", shopGoodsVos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "shoppingCar";
    }
    
    
    
    public void clear(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session=req.getSession();
        User cuser =(User) session.getAttribute("cuser");
        session.removeAttribute("shopping_"+cuser.getId());
        try {
            ResponseUtil.writeJson(resp, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 

 

 

 拜拜拜

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/297991.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号