ショッピングカートのパッケージエンティティオブジェクト

ショッピングカート

パッケージエンティティオブジェクト

これは、ショッピングアイテムです

package com.itheima.shop.cart;

import com.itheima.shop.product.Product;

public class CartItem {
	// 商品对象
	private Product product;
	//数量
	private Integer count;
	//小计
	private Double subtotal;
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public Integer getCount() {
		return count;
	}
	public void setCount(Integer count) {
		this.count = count;
	}
	public Double getSubtotal() {
		return count * product.getShop_price();
	}
}

ショッピングカート

package com.itheima.shop.cart;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Cart {
	// 购物车存放多个购物项
	// Map集合用商品的ID作为Map的key,购物项作为Map的value
	private Map<Integer, CartItem> map = new HashMap<Integer, CartItem>();

	// 提供获得Map的value的集合
	// 相当于一个属性:cartItems
	public Collection getCartItems() {
		return map.values();
	}

	// 总计;
	private Double total = 0d;
	public Double getTotal() {
		return total;
	}

	// 提供三个方法
	public void addCart(CartItem cartItem) {
		Integer pid = cartItem.getProduct().getPid();
		if (map.containsKey(pid)) {
			CartItem _cartItem = map.get(pid);
			_cartItem.setCount(cartItem.getCount() + cartItem.getCount());
		} else {
			map.put(pid, cartItem);
		}
		// 总计
		total += cartItem.getSubtotal();
	}

	public void removeCart(Integer pid) {
		CartItem cartItem = map.remove(pid);
		total -= cartItem.getSubtotal();
	}

	public void clearCart() {
		// 清空Map
		map.clear();
		total = 0d;
	}
}

セッション中にカプセル化されたエンティティ

package com.itheima.shop.cart;



import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itheima.shop.product.Product;
import com.itheima.shop.product.ProductService;
import com.opensymphony.xwork2.ActionSupport;

public class CartAction extends ActionSupport {
	private Integer pid;
	private Integer count;
	private ProductService productService;

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public void setCount(Integer count) {
		this.count = count;
	}
    //把cart 封装到 session中。
	public Cart getCart(HttpServletRequest request) {
       Cart cart = (Cart) request.getSession().getAttribute("cart"); 		
       if(cart == null) {
    	    cart = new Cart();
    	   request.getSession().setAttribute("cart", cart);
       }
       return cart;
       
	}
	public String addCart() {
		// 创建一个CartItem
		Product product = productService.findByPid(pid);
		CartItem cartItem = new CartItem();
		cartItem.setCount(count);
		cartItem.setProduct(product);
        //获取购物车需要依赖request对象
		HttpServletRequest request = ServletActionContext.getRequest();
		Cart cart = getCart(request);
		cart.addCart(cartItem);
		
		return "addCartSuccess";
	}
	//清空购物车
	public String clearCart() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Cart cart = getCart(request);
		cart.clearCart();
		
		return "clearCartSuccess";
	}
	//删除购物xaing
	public String cartRemove() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Cart cart = getCart(request);
		cart.removeCart(pid);
		return "removeSuccess";
	}
}

<s:iterator value="#session.cart == null || #session.cart.cartItems" >
						<tr>
							<td width="60">
								<input type="hidden" name="id" value="22">
								<img src="${pageContext.request.contextPath}/<s:property value="product.image" />">
							</td>
							<td>
								<a target="_blank"><s:property value="product.pname" /></a>
							</td>
							<td><s:property value="product.shop_price" />
							</td>
							<td class="quantity" width="60">
								<input type="text" name="count" value="<s:property value="count" />" maxlength="4" onpaste="return false;">
								<div>
									<span class="increase">&nbsp;</span>
									<span class="decrease">&nbsp;</span>
								</div>
							</td>
							<td width="140">
								<span class="subtotal"><s:property value="subtotal" /></span>
							</td>
							<td>
								<a href="${pageContext.request.contextPath }/cart_cartRemove.action?pid=<s:property value="product.pid" /> " class="delete">删除</a>
							</td>
						</tr>
						</s:iterator>

以下のためのページ。

おすすめ

転載: blog.csdn.net/qq_41922566/article/details/94761968