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

📄 shoppingcartaction.java

📁 购物车系统
💻 JAVA
字号:
package com.estore.web.action;
import java.util.*;

import javax.servlet.http.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.MappingDispatchAction;

import com.estore.biz.ProductBiz;
import com.estore.entity.*;

public class ShoppingCartAction extends MappingDispatchAction{
	
	 public ActionForward list(ActionMapping mapping, 
             ActionForm form, 
               HttpServletRequest request, 
                 HttpServletResponse response) throws Exception
     {
		 ActionForward forward = null;
		 ProductBiz biz = new ProductBiz();
		 try{
			 List<Product> pros = biz.listProduct();
		     request.setAttribute("pros", pros);
		     forward = mapping.findForward("list"); 
		 }catch(Exception e){
			 e.printStackTrace();
			 forward = mapping.findForward("error"); 
		 }
		return forward;
     }
	 
	 
	 
	 public ActionForward addItem(ActionMapping mapping, 
             ActionForm form, 
               HttpServletRequest request, 
                 HttpServletResponse response) throws Exception
     {
		 ActionForward forward = null;
		 ProductBiz biz = new ProductBiz();
		 try{
			 
			 Integer  id=Integer.valueOf(request.getParameter("id"));
			 Product p = biz.findProductById(id);
			 HttpSession session=request.getSession(false);
			 if(session.getAttribute("cart")==null){
				 Cart cart = new Cart();
				 session.setAttribute("cart",cart);
				 cart.addItem(p, id);
			 }else{
				 Cart cart = (Cart)session.getAttribute("cart");
				 cart.addItem(p, id);
			 }
			 forward =  mapping.findForward("addItem"); 
		 }catch(Exception e){
			 e.printStackTrace();
			 forward = mapping.findForward("error");
		 }
		 return forward;
     }
	 
	 
	 public ActionForward listCart(ActionMapping mapping, 
             ActionForm form, 
               HttpServletRequest request, 
                 HttpServletResponse response) throws Exception
     {
		 ActionForward forward = null;
		 try{
			 HttpSession session=request.getSession(false);
			 Cart cart = (Cart)session.getAttribute("cart");
			 session.setAttribute("cart",cart);
			 forward = mapping.findForward("listCart");
		 }catch(Exception e){
			 e.printStackTrace();
			 forward = mapping.findForward("error");
		 }
		 return forward;
     }
	 
	 public ActionForward modifyNumber(ActionMapping mapping, 
             ActionForm form, 
             HttpServletRequest request, 
               HttpServletResponse response)throws Exception
		{
		 ActionForward forward = null;
		 try{
			HttpSession session=request.getSession(false);
			Cart cart = (Cart)session.getAttribute("cart");
			String[] productId=request.getParameterValues("productId");
			String[] number=request.getParameterValues("num");
			for(int i=0; i<number.length; i++){
				cart.modifyNumberByProductId(Integer.valueOf(productId[i]), Integer.parseInt(number[i]));
			}
			session.setAttribute("cart",cart);
			forward = mapping.findForward("modifyNumber");
		 }catch(Exception e){
			 e.printStackTrace();
			 forward = mapping.findForward("error");
		 }
		 return forward;
	}
	 public ActionForward deleteSelectedItem(ActionMapping mapping, 
             ActionForm form, 
             HttpServletRequest request, 
               HttpServletResponse response)throws Exception
		{
		 ActionForward forward = null;
		 try{
				HttpSession session=request.getSession(false);
				Cart cart = (Cart)session.getAttribute("cart");
				String[] productId=request.getParameterValues("productId");
				String[] items = request.getParameterValues("itemsId");
				
				for(int i=0; i<items.length; i++){
					if(items[i] != null){
					cart.deleteItemByProductId(Integer.parseInt(productId[i]));
					}
				if(cart.isEmpty())
					cart.setCost(0);
				}
				session.setAttribute("cart",cart);
				forward = mapping.findForward("deleteSelectedItem");
		 }catch(Exception e){
			 e.printStackTrace();
			 forward = mapping.findForward("error");
		 }
		 return forward;
	}
	 
	 public ActionForward clearCart(ActionMapping mapping, 
             ActionForm form, 
             HttpServletRequest request, 
               HttpServletResponse response)throws Exception
		{
		 ActionForward forward = null;
		 try{
				HttpSession session =request.getSession(false);
				Cart  cart=(Cart) session.getAttribute("cart");
				cart=new Cart();
				session.setAttribute("cart", cart);
				forward=mapping.findForward("clearCart");
			}catch(Exception e){
				e.printStackTrace();
				forward=mapping.findForward("error");
			}
		 return forward;
	}
	 
	 public ActionForward previewOrder(ActionMapping mapping, 
             ActionForm form, 
             HttpServletRequest request, 
               HttpServletResponse response)throws Exception{
		 ActionForward forward = null;
		 try{
			 HttpSession session =request.getSession(false);
			 User user=(User)session.getAttribute("user");
			 Cart cart=(Cart)session.getAttribute("cart");
			 if(user == null){
				 ActionErrors errors = new ActionErrors();
				 ActionMessage message = new ActionMessage("errors.nologin");     
				 errors.add("message", message);
				 saveErrors(session, errors);
				 forward = mapping.findForward("nologin");
			 }else{
				 session.setAttribute("user", user);
				// session.setAttribute("cart", cart);
				 forward = mapping.findForward("previewOrder");
			 }
		 }catch(Exception e){
			 e.printStackTrace();
			 forward=mapping.findForward("error");
		 }
		 
		 return forward;
	}
	 
	 public ActionForward generateOrder(ActionMapping mapping, 
             ActionForm form, 
             HttpServletRequest request, 
               HttpServletResponse response)throws Exception{
		 ActionForward forward = null;
		 ProductBiz biz = new ProductBiz();
		 try{
				HttpSession session =request.getSession(false);
				User user=(User)session.getAttribute("user");
				Cart cart=(Cart)session.getAttribute("cart");
				HashSet<Item> items=cart.getValue();
				Order order=new Order();
				order.setUser(user);
				order.setItem(items);
				order.setStatus(0);
				order.setCost(cart.getCost());
				System.out.println(cart.getCost());
				int preview =biz.generateOrder(order);
				request.setAttribute("message", preview);
				session.setAttribute("order", order);
				session.setAttribute("cart", null);
				forward=mapping.findForward("generateOrder");
			}catch(Exception e){
				e.printStackTrace();
				forward= mapping.findForward("error");
			}
		 
		 return forward;
	 }
	 
}

⌨️ 快捷键说明

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