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

📄 cartaction.java

📁 模仿当当网基于struts+hierbernate与mysql的商务网站。
💻 JAVA
字号:
package org.whatisjava.dang.action;

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

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.MappingDispatchAction;
import org.whatisjava.dang.domain.User;
import org.whatisjava.dang.form.CartItemForm;
import org.whatisjava.dang.service.CartService;
import org.whatisjava.dang.service.WrongProductNumException;
import org.whatisjava.dang.util.Constants;
import org.whatisjava.dang.util.CookieUtils;

public class CartAction extends MappingDispatchAction {
	Logger logger = Logger.getLogger(this.getClass());

	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartService cs = getCartService(request);
		if (cs.isEmpty()) {
			logger.info("购物车 空");
			ActionMessages errors = new ActionMessages();
			errors.add("cartEmpty", new ActionMessage("error.cartEmpty"));
			saveErrors(request, errors);
		}
		saveToCookie(response, cs, request.getContextPath());
		logger.debug("购物车:" + cs);
		return mapping.findForward("success");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartService cs = getCartService(request);
		CartItemForm f = (CartItemForm) form;
		boolean b = cs.addItem(f.getProductId());
		response.getWriter().write(b ? '1' : '0');
		return null;
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartService cs = getCartService(request);
		CartItemForm f = (CartItemForm) form;
		try {
			cs.updateNum(f.getProductId(), f.getNum());
		} catch (WrongProductNumException e) {
			logger.error(e);
			ActionMessages messages=new ActionMessages();
			messages.add("numError"+e.getProductId(),new ActionMessage("error.wrongProductNum"));
//			messages.add("errorName",new ActionMessage("error.wrongProductNum"));
//			messages.add("errorMessage",new ActionMessage("error.wrongProductNum.message"));
			saveErrors(request,messages);
//			return mapping.findForward("error");
			return mapping.getInputForward();
		}
		return mapping.findForward("success");
	}

	public ActionForward drop(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartService cs = getCartService(request);
		CartItemForm f = (CartItemForm) form;
		cs.drop(f.getProductId());
		return mapping.findForward("success");
	}

	public ActionForward recovery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartService cs = getCartService(request);
		CartItemForm f = (CartItemForm) form;
		cs.recovery(f.getProductId());
		return mapping.findForward("success");
	}

	private CartService getCartService(HttpServletRequest request)
			throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Constants.S_USER);
		CartService cs = (CartService) session.getAttribute(Constants.S_CART);
		if (cs == null) {
			cs = new CartService(user);
			String cartStr = CookieUtils.findCookie(request,
					CookieUtils.CART_ID);
			if (cartStr != null) {
				cs.load(cartStr);
			}
		}
		session.setAttribute(Constants.S_CART, cs);
		return cs;
	}

	private void saveToCookie(HttpServletResponse response, CartService cs,
			String path) throws Exception {
		CookieUtils.addCookie(response, CookieUtils.CART_ID, cs.serialize(),
				path, 60 * 60 * 24 * 60);
	}
}

⌨️ 快捷键说明

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