2021-10-09购物车功能

一加入购物车

package com.zking.vo;
 
public class ShoppingVo {
	private String name;
	private float price;
	private int num;
	private float total;
 
	private String consignee;
	private String phone;
	private String postalcode;
	private String address;
	private int sendType;
 
	private String pageStr;
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public float getPrice() {
		return price;
	}
 
	public void setPrice(float price) {
		this.price = price;
	}
 
	public int getNum() {
		return num;
	}
 
	public void setNum(int num) {
		this.num = num;
	}
 
	public float gettotal() {
		return total;
	}
 
	public void settotal(float total) {
		this.total = total;
	}
 
	public String getConsignee() {
		return consignee;
	}
 
	public void setConsignee(String consignee) {
		this.consignee = consignee;
	}
 
	public String getPhone() {
		return phone;
	}
 
	public void setPhone(String phone) {
		this.phone = phone;
	}
 
	public String getPostalcode() {
		return postalcode;
	}
 
	public void setPostalcode(String postalcode) {
		this.postalcode = postalcode;
	}
 
	public String getAddress() {
		return address;
	}
 
	public void setAddress(String address) {
		this.address = address;
	}
 
	public int getSendType() {
		return sendType;
	}
 
	public void setSendType(int sendType) {
		this.sendType = sendType;
	}
 
	public String getPageStr() {
		return pageStr;
	}
 
	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}
 
	@Override
	public String toString() {
		return "ShoppingVo [name=" + name + ", price=" + price + ", num=" + num + ", total=" + total + ", consignee="
				+ consignee + ", phone=" + phone + ", postalcode=" + postalcode + ", address=" + address + ", sendType="
				+ sendType + ", pageStr=" + pageStr + "]";
	}
 
	public ShoppingVo(String name, float price, int num, float total, String consignee, String phone, String postalcode,
			String address, int sendType, String pageStr) {
		super();
		this.name = name;
		this.price = price;
		this.num = num;
		this.total = total;
		this.consignee = consignee;
		this.phone = phone;
		this.postalcode = postalcode;
		this.address = address;
		this.sendType = sendType;
		this.pageStr = pageStr;
	}
	public ShoppingVo() {
		// TODO Auto-generated constructor stub
	}
 
}

2.购物车没有相应的数据库表,所以没有dao类

3.子控制器内

public String add(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session = req.getSession();
        User user = (User)session.getAttribute("cuser");
        ObjectMapper om=new ObjectMapper();
        if(user!=null) {
            long id = user.getId();
            List<ShoppingVo> shopGoodsVos=null;
            String info =(String) session.getAttribute("shopping_"+id);
            if(StringUtils.isNotBlank(info)) {
                try {
                    shopGoodsVos=om.readValue(info, List.class);
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                shopGoodsVos=new ArrayList<>();
            }
            shopGoodsVos.add(vo);
            try {
                session.setAttribute("shopping_"+id, om.writeValueAsString(shopGoodsVos));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            req.setAttribute("shopGoodsVos", shopGoodsVos);
        }
        return "shoppingCar";
    }

 4.mvc文件的配置

<action path="/shopping" type="com.zking.web.ShoppingVoAction"> 
           <forward name="shoppingCar" path="/fg/shoppingCar.jsp" redirect="false" /> 
         </action>

二、查询购物车

1.通过session查询

public String list(HttpServletRequest req, HttpServletResponse resp) {
		HttpSession session = req.getSession();
		User user = (User)session.getAttribute("cuser");
		ObjectMapper om=new ObjectMapper();
		String info =(String) session.getAttribute("shopping_"+user.getId());
		try {
			List<ShoppingVo> shopGoodsVos=om.readValue(info, List.class);
			req.setAttribute("shopGoodsVos", shopGoodsVos);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "shoppingCar";
	}

三、清空购物车 

1.子控制器内

public void clear(HttpServletRequest req, HttpServletResponse resp) throws JsonProcessingException, IOException {
		HttpSession session = req.getSession();
		User user = (User)session.getAttribute("cuser");
		session.removeAttribute("shopping_"+user.getId());
		ResponseUtil.writeJson(resp, "1");
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以帮你实现一个简单的购物车页面。首先需要引入 ElementUI 库,然后创建一个购物车组件,里面包含商品列表和购物车列表。具体代码如下: ```vue <template> <div> <h2>商品列表</h2> <el-row> <el-col v-for="item in goods" :key="item.id" :span="6"> <el-card :body-style="{ padding: '10px' }"> <img :src="item.img" style="width: 100%"> <div style="height: 60px"> <span style="float: left">{{ item.name }}</span> <span style="float: right">{{ item.price }}元</span> </div> <el-button type="primary" @click="addCart(item)">加入购物车</el-button> </el-card> </el-col> </el-row> <h2>购物车列表</h2> <el-table :data="cart" style="width: 100%"> <el-table-column prop="name" label="商品名称"></el-table-column> <el-table-column prop="price" label="单价"></el-table-column> <el-table-column prop="count" label="数量"></el-table-column> <el-table-column prop="total" label="总价"></el-table-column> </el-table> </div> </template> <script> export default { data() { return { goods: [ { id: 1, name: '商品1', price: 10, img: 'https://picsum.photos/id/1/200/200' }, { id: 2, name: '商品2', price: 20, img: 'https://picsum.photos/id/2/200/200' }, { id: 3, name: '商品3', price: 30, img: 'https://picsum.photos/id/3/200/200' }, { id: 4, name: '商品4', price: 40, img: 'https://picsum.photos/id/4/200/200' }, ], cart: [], }; }, methods: { addCart(item) { const index = this.cart.findIndex((cartItem) => cartItem.id === item.id); if (index === -1) { this.cart.push({ ...item, count: 1, total: item.price }); } else { this.cart[index].count += 1; this.cart[index].total += item.price; } }, }, }; </script> ``` 这段代码创建了一个包含商品列表和购物车列表的页面,商品列表中包含4个商品,每个商品有名称、价格和图片,以及一个“加入购物车”按钮。购物车列表展示已添加的商品,包括名称、单价、数量和总价。 当点击“加入购物车”按钮时,会将商品添加到购物车列表中。如果购物车中已经存在该商品,则数量加1并更新总价;否则将该商品添加到购物车列表中,并将数量设置为1,总价为单价。 这是一个简单的购物车实现,你可以根据自己的需求进行修改和完善。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值