一、加入购物车
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<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.sjy.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("user");
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("user");
session.removeAttribute("shopping_"+user.getId());
ResponseUtil.writeJson(resp, "1");
}
结果图