📄 cartaction.java
字号:
package org.whatisjava.dang.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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.util.Constants;
import org.whatisjava.dang.util.CookieUtils;
public class CartAction extends MappingDispatchAction {
public ActionForward show(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
CartService cs = getCartService(request);
if (cs.isEmpty()) {
ActionMessages errors = new ActionMessages();
errors.add("error.cart.empty",
new ActionMessage("error.cart.empty"));
saveErrors(request, errors);
}
saveToCookie(response, 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;
cs.updateNum(f.getProductId(), f.getNum());
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.SID_USER);
CartService cs = (CartService) session.getAttribute(Constants.SID_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.SID_CART, cs);
return cs;
}
private void saveToCookie(HttpServletResponse response, CartService cs)
throws Exception {
CookieUtils.addCookie(response, CookieUtils.CART_ID, cs.serialize());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -