⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addproducttocartaction.java

📁 基于Struts的网络商店源码。使用Hibernate技术
💻 JAVA
字号:
package com.sush.webstore.store.web.struts;

import java.util.Map;

//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpSession;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sush.webstore.store.domain.ICart;
import com.sush.webstore.store.domain.IProduct;
import com.sush.webstore.store.domain.IWebStoreFacade;
import com.sush.webstore.store.domain.facade.WebStorePOJO;

public class AddProductToCartAction extends ActionSupport {

	private long id;

	private long quantity = 1;

	private IWebStoreFacade webStore = new WebStorePOJO();

	private ICart cart;

	public String execute() {

		if (!init())
			return "error";

		IProduct product = webStore.getProduct(id);

		if (product == null)
			return "error";

		return (cart.addProduct(product, quantity)) ? "success" : "error";
	}

	/**
	 * @return the cart
	 */
	public boolean init() {

		// Map req = (Map) ActionContext.getContext().get("request");
		//
		// if (req == null) {
		// System.out.println("(req == null)");
		// return false;
		// }
		//
		// System.out.println("req != null");
		// HttpServletRequest request = (HttpServletRequest) req;
		// HttpSession session = request.getSession(true);

		Map session = (Map) ActionContext.getContext().get("session");

		if (session == null) {
			return false;
		}

		// cart = (ICart) session.getAttribute("cart");
		cart = (ICart) session.get("cart");

		if (cart == null) {
			cart = webStore.createCart();
			// session.setAttribute("cart", cart);
			session.put("cart", cart);
		}
		return true;
	}

	/**
	 * @return the cart
	 */
	public ICart getCart() {
		return cart;
	}

	/**
	 * @param cart
	 *            the cart to set
	 */
	public void setCart(ICart cart) {
		this.cart = cart;
	}

	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @return the webStore
	 */
	public IWebStoreFacade getWebStore() {
		return webStore;
	}

	/**
	 * @param webStore
	 *            the webStore to set
	 */
	public void setWebStore(IWebStoreFacade webStore) {
		this.webStore = webStore;
	}

	/**
	 * @return the quantity
	 */
	public long getQuantity() {
		return quantity;
	}

	/**
	 * @param quantity
	 *            the quantity to set
	 */
	public void setQuantity(long quantity) {
		this.quantity = quantity;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -