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

购物车功能

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

购物车功能

一、加入购物车

1.购物车实体类

package com.sjy.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("user");
		ObjectMapper om=new ObjectMapper();
		if(user!=null) {
			long id = user.getId();
			List 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文件的配置

  
 	       
 	    
二、查询购物车

1.通过session查询

public String list(HttpServletRequest req, HttpServletResponse resp) {
		HttpSession session = req.getSession();
		User user = (User)session.getAttribute("user");
		ObjectMapper om=new ObjectMapper();
		String info =(String) session.getAttribute("shopping_"+user.getId());
		try {
			List 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("user");
		session.removeAttribute("shopping_"+user.getId());
		ResponseUtil.writeJson(resp, "1");
	}

结果图

 

 

 

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

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

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