前言:購物車實作方式常用的有:利用Session 、Cookie、資料庫等。本文使用Session 實作購物車功能。 利用Cookie實作購物車可參考本人以前寫的Session Cookie 實作記錄使用者上次通路時間&購物車
購物車實作
當使用者登入購買商品時,建立List集合,将商品添加到集合中,然後将使用者ID與集合儲存到Session 中。
購物車實體類(Shopping)
package com.wyy.entity;
public class Shopping {
// 購物車清單訂單項所需資料
private int id;
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 int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 "Shopping [id=" + id + ", name=" + name + ", price=" + price + ", num=" + num + ", total=" + total
+ ", consignee=" + consignee + ", phone=" + phone + ", postalcode=" + postalcode + ", address=" + address
+ ", sendType=" + sendType + ", pageStr=" + pageStr + "]";
}
public Shopping() {
// TODO Auto-generated constructor stub
}
public Shopping(int id, String name, float price, int num, float total, String consignee, String phone,
String postalcode, String address, int sendType, String pageStr) {
super();
this.id = id;
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;
}
}
前端頁面
<a type="button" class="btn btn-danger" href="${pageContext.request.contextPath}/Shopping.action?methodName=shoppingCar&name=${b.name}&price=${b.price}&num=1&id=${b.id}" target="_blank" rel="external nofollow" >加入購物車</a>
子控制器
當第一次添加商品時,建立一個集合将商品放到集合中,當使用者再次添加時,判斷集合中是否有該商品。有則數量加一,沒有則添加到集合中
/**
* 添加到購物車
*
* @param req
* @param resp
* @return
*/
public String shoppingCar(HttpServletRequest req, HttpServletResponse resp) {
User u = (User) req.getSession().getAttribute("user");
if (u == null) {
return "login";
}
List<Shopping> ShoppingCar = (List<Shopping>) req.getSession().getAttribute(String.valueOf(u.getId()));
boolean b = true;
if (ShoppingCar != null && ShoppingCar.size() > 0) {
for (Shopping shopping : resultCar) {
if (shopping.getId() == s.getId()) {
shopping.setNum(shopping.getNum() + 1);
b = false;
}
}
if (b) {
ShoppingCar.add(s);
}
req.getSession().setAttribute(String.valueOf(u.getId()), ShoppingCar);
req.setAttribute("ss", ShoppingCar);
} else {
List<Shopping> list = new ArrayList<Shopping>();
list.add(s);
req.getSession().setAttribute(String.valueOf(u.getId()), list);
req.setAttribute("ss", list);
}
return "Car";
}
購物車跳轉
頁面按鈕
<a type="button" class="text-primary" href="${pageContext.request.contextPath}/Shopping.action?methodName=list" target="_blank" rel="external nofollow" >我的購物車</a>
子控制器
/**
* 購物車顯示
* @param req
* @param resp
* @return
*/
public String list(HttpServletRequest req, HttpServletResponse resp) {
User u = (User) req.getSession().getAttribute("user");
if (u == null) {
return "login";
}
List<Shopping> ShoppingCar = (List<Shopping>) req.getSession().getAttribute(String.valueOf(u.getId()));
req.setAttribute("ss", ShoppingCar);
return "Car";
}
清空購物車
//清除購物車
function clearCar() {
$.messager.confirm('确認', '您确認想要清空購物車嗎?', function (r) {
if (r) {
$.ajax({
url: '${pageContext.request.contextPath}/Shopping.action?methodName=clear',
success: function (data) {
location.href = '${pageContext.request.contextPath}/Shopping.action?methodName=list';
}
});
}
})
}
子控制器
/**
* 清空購物車
* @param req
* @param resp
* @return
*/
public String clear(HttpServletRequest req, HttpServletResponse resp) {
User u = (User) req.getSession().getAttribute("user");
if (u == null) {
return "login";
}
req.getSession().removeAttribute(String.valueOf(u.getId()));
return null;
}