购物车功能

实现购物的三种方式:

分析:
1.session保存购物车信息到session(服务端)
思路:

①.点击我的购物车,查询后台的session,通过用户账号去找

②.如果有那就显示商品,没有则不显示

③.添加购物车,将数据加入两个地方:前台HTML显示,后台的session

        通过userid进行保存session内容

④.清空购物车,清空session

        注意:所有购物车相关的操作应该先登录

2.cookie保存购物车(效率要更高、客户端、不安全)
3.数据库

一.加入购物车

①.建购物车实体类

package 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;
}
  public ShoppingVo() {
	// TODO Auto-generated constructor stub
}

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;
}

@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 + "]";
}
  
}

②.子控制器

/**
 * 将信息加入购物车
 * @param req
 * @param resp
 * @return
 */
		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) {
					/*点击添加传递到后台的是一个对象,然后购物车需要的是list集合进行显示

					1、第一次添加购物车是没有数据的,也就意味着把vo放到list集合shopGoodsVos中,传递到前台即可

					2、第二次添加到购物车,也就意味着之前有购物车相关信息

					取出原有的购物车信息list 集合,将本次添加购物车的实体类vo放到原有的购物车信息list集合shopGoodsVos中*/
					long uid = cuser.getId();
					List<ShoppingVo> shopGoodsVos=null;
					//从session取出当前用户的购物车信息
					String shoppingInfo=(String)session.getAttribute("shopping_"+uid);
					if(StringUtils.isNotBlank(shoppingInfo)) {
						shopGoodsVos=om.readValue(shoppingInfo, List.class);
				//第2、3次添加
					}else {
						//第一次添加
						shopGoodsVos=new ArrayList<ShoppingVo>();
					}
					//vo指的是前台点击购物车具体内容商品
					shopGoodsVos.add(vo);
					session.setAttribute("shopping"+uid, om.writeValueAsString(shopGoodsVos));
					req.setAttribute("shopGoodsVos", shopGoodsVos);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "shoppingCar";
		}
		

③.配置

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

二.查询购物车

/**
		 * 查询购物车
		 * @param req
		 * @param resp
		 * @return
		 */
		public String list(HttpServletRequest req, HttpServletResponse resp) {
			HttpSession session=req.getSession();
			User cuser = (User) session.getAttribute("cuser");
			ObjectMapper om=new ObjectMapper();
			String shoppingInfo=(String)session.getAttribute("shopping_"+cuser.getId());
			try {
				List<ShoppingVo> shopGoodsVos = om.readValue(shoppingInfo, List.class);
				req.setAttribute("shopGoodsVos", shopGoodsVos);
			} catch (Exception 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) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值