?? cartaction.java
字號:
package com.estore.struts.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.actions.DispatchAction;import com.estore.struts.Globals;import com.estore.struts.entity.Cart;import com.estore.struts.entity.CartProduct;import com.estore.struts.entity.Product;import com.estore.struts.entity.User;import com.estore.struts.service.ProductService;import com.estore.struts.utils.ServiceFactory;public class CartAction extends DispatchAction { public ActionForward addCart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { /* * 在添加商品之前,會對session和user進行判斷,如果不存在時,會把錯誤信息封裝到request中, * 然后轉發(fā)出去讓用戶重新登錄; */ HttpSession session = request.getSession(false); String message = ""; if (session == null) { message = "該Session已經(jīng)過期,或者你沒有登錄,請重新<a href='/estore/pages/login.jsp' style='color:red;'>登錄</a>!請<a onclick='javascript:history.go(-1)' style='color:red;'>返回</a>!"; request.setAttribute("message", message); return mapping.findForward("error"); } User user = (User) session.getAttribute("user"); if (user == null) { message = "該Session已經(jīng)過期,或者你沒有登錄,請重新<a href='/estore/pages/login.jsp' style='color:red;'>登錄</a>! 請<a onclick='javascript:history.go(-1)' style='color:red;'>返回</a>"; request.setAttribute("message", message); return mapping.findForward("error"); } /* * 從session中獲得購物車 */ Cart cart = (Cart) session.getAttribute("cart"); if (cart == null) { cart = new Cart(); } /* * 從數(shù)據(jù)庫中把商品取到; */ ProductService productService = (ProductService) ServiceFactory .getInstance().getService(Globals.PRODUCT_SERVICE); Integer id = Integer.parseInt(request.getParameter("productid")); Product product = productService.getProductById(id); /* * 在向購物車中添加商品的時候會判斷商品是否已經(jīng)存在, * 已存在的就不讓在加入了; */ if (cart.isExist(id)) { message = "該商品已經(jīng)存在!請<a onclick='javascript:history.go(-1)'>返回</a>!"; request.setAttribute("message", message); return mapping.findForward("error"); } else { /* * 向購物車添加一個商品; */ cart.addCart(product); session.setAttribute("cart", cart); return mapping.findForward("addcartsuccess"); } } public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession session = request.getSession(); Integer productid = Integer.parseInt(request.getParameter("productid")); ProductService productService = (ProductService) ServiceFactory .getInstance().getService(Globals.PRODUCT_SERVICE); Product pro = productService.getProductById(productid); Cart cart = (Cart) session.getAttribute("cart"); cart.removeCart(pro); session.setAttribute("cart", cart); return mapping.findForward("removecartsuccess"); } public ActionForward modify(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Integer productid=Integer.parseInt(request.getParameter("productid")); int num=Integer.parseInt(request.getParameter("num")); HttpSession session = request.getSession(); Cart cart = (Cart) session.getAttribute("cart"); cart.modifyNum(productid, num); return mapping.findForward("modifycartsuccess"); } /* * 也就是從session中把購物車清除掉; */ public ActionForward removeall(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession session = request.getSession(); Cart cart = (Cart) session.getAttribute("cart"); session.removeAttribute("cart"); return mapping.findForward("success"); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -