cartmanageaction.java
来自「一个很好的网上商城系统」· Java 代码 · 共 226 行
JAVA
226 行
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.hb.shop.web.struts.action;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
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 org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hb.shop.dao.OrderWaresDAO;
import com.hb.shop.dao.OrdersDAO;
import com.hb.shop.dao.UsersDAO;
import com.hb.shop.dao.WaresDAO;
import com.hb.shop.model.OrderWares;
import com.hb.shop.model.Orders;
import com.hb.shop.model.Users;
import com.hb.shop.model.Wares;
/**
* MyEclipse Struts
* Creation date: 10-18-2008
*
* XDoclet definition:
* @struts.action parameter="method" validate="true"
*/
public class CartManageAction extends DispatchAction {
ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
WaresDAO wareDao = (WaresDAO) act.getBean("WaresDAO");
OrderWaresDAO orderWaresDAO = (OrderWaresDAO) act.getBean("OrderWaresDAO");
UsersDAO usersDAO = (UsersDAO) act.getBean("UsersDAO");
OrdersDAO ordersDAO = (OrdersDAO) act.getBean("OrdersDAO");
List cartList;
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward addCartItem(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String wareId =request.getParameter("wareId");
String count = request.getParameter("count");
System.out.println(wareId+"---"+count);
HttpSession session = request.getSession();
if(session.getAttribute("cartList")==null){
cartList = new ArrayList();
}else{
cartList = (List) session.getAttribute("cartList");
}
OrderWares tempOrderWare;
for(int i=0;i<cartList.size();i++){
tempOrderWare = (OrderWares) cartList.get(i);
if(tempOrderWare.getWares().getWareId()==Integer.parseInt(wareId)){
tempOrderWare.setWareNum(tempOrderWare.getWareNum()+Integer.parseInt(count));
tempOrderWare.setWarePrice(tempOrderWare.getWarePrice()+Float.parseFloat(count)*tempOrderWare.getWares().getWarePrice());
float account = 0 ;
for(int j=0;j<cartList.size();j++){
account = account + ((OrderWares)cartList.get(j)).getWarePrice();
}
session.setAttribute("account", account);
return mapping.findForward("cartList");
}
}
Wares tempWare = wareDao.findById(Integer.parseInt(wareId));
float warePrice = Float.parseFloat(count)*tempWare.getWarePrice();
OrderWares orderWare = new OrderWares();
orderWare.setWares(tempWare);
orderWare.setWareNum(Integer.parseInt(count));
orderWare.setWarePrice(warePrice);
cartList.add(orderWare);
float account = 0 ;
for(int i=0;i<cartList.size();i++){
account = account + ((OrderWares)cartList.get(i)).getWarePrice();
}
session.setAttribute("account", account);
session.setAttribute("cartList", cartList);
return mapping.findForward("cartList");
}
public ActionForward editCartItem(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String wareId = request.getParameter("wareId");
String wareNum = request.getParameter("wareNum");
System.out.println("editCartItem---"+wareId+"---"+wareNum);
HttpSession session = request.getSession();
cartList = (List) session.getAttribute("cartList");
OrderWares tempOrderWare;
for(int i=0;i<cartList.size();i++){
tempOrderWare = (OrderWares) cartList.get(i);
if(tempOrderWare.getWares().getWareId()==Integer.parseInt(wareId)){
tempOrderWare.setWareNum(Integer.parseInt(wareNum));
float warePrice = Float.parseFloat(wareNum)*tempOrderWare.getWares().getWarePrice();
tempOrderWare.setWarePrice(warePrice);
float account = 0 ;
for(int j=0;j<cartList.size();j++){
account = account + ((OrderWares)cartList.get(j)).getWarePrice();
}
session.setAttribute("account", account);
return mapping.findForward("cartList");
}
}
return mapping.findForward("cartList");
}
public ActionForward deleteCartItem(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String wareId = request.getParameter("wareId");
System.out.println("deleteCartItem---"+wareId);
HttpSession session = request.getSession();
cartList = (List) session.getAttribute("cartList");
OrderWares tempOrderWare;
for(int i=0;i<cartList.size();i++){
tempOrderWare = (OrderWares) cartList.get(i);
if(tempOrderWare.getWares().getWareId()==Integer.parseInt(wareId)){
cartList.remove(tempOrderWare);
float account = 0 ;
for(int j=0;j<cartList.size();j++){
account = account + ((OrderWares)cartList.get(j)).getWarePrice();
}
session.setAttribute("account", account);
return mapping.findForward("cartList");
}
}
return mapping.findForward("cartList");
}
public ActionForward deleteCart(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("deleteCart");
HttpSession session = request.getSession();
if(session.getAttribute("cartList")==null){
return mapping.findForward("cartList");
}
cartList = (List) session.getAttribute("cartList");
cartList.clear();
float account = 0 ;
for(int j=0;j<cartList.size();j++){
account = account + ((OrderWares)cartList.get(j)).getWarePrice();
}
session.setAttribute("account", account);
return mapping.findForward("cartList");
}
public ActionForward commitOrder(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("commitOrder");
String orderCarry = request.getParameter("orderCarry");
String orderPay = request.getParameter("orderPay");
HttpSession session = request.getSession();
Integer userId = (Integer) session.getAttribute("userId");
Users user = usersDAO.findById(userId);
//System.out.println(user.getUserId());
List orderWareList = (List) session.getAttribute("cartList");
String totalPrice = String.valueOf(session.getAttribute("account"));
//System.out.println(totalPrice);
float totalPriceFloat = 0.0f + Float.parseFloat(totalPrice);
//System.out.println(totalPriceFloat);
Orders order = new Orders();
order.setUsers(user);
order.setOrderTotalprice(totalPriceFloat);
order.setOrderDate(new java.util.Date());
order.setOrderState("未发送");
order.setOrderInfo("新订单");
order.setOrderCarry(orderCarry);
order.setOrderPay(orderPay);
ordersDAO.save(order);
//Set wareOderSet = new HashSet();
OrderWares tempOrderWare;
for(int i=0;i<orderWareList.size();i++){
tempOrderWare = (OrderWares) orderWareList.get(i);
//wareOderSet.add(tempOrderWare);
tempOrderWare.setOrders(order);
orderWaresDAO.save(tempOrderWare);
}
orderWareList.clear();
session.setAttribute("account", 0.0f);
//order.setOrderWareses(wareOderSet);
return mapping.findForward("checkOK");
}
public ActionForward cartIsNull(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("cartIsNull");
HttpSession session = request.getSession();
List orderWareList = (List) session.getAttribute("cartList");
if(session.getAttribute("cartList")==null||orderWareList.isEmpty()){
request.setAttribute("cartIsNull", "isNull");
return mapping.findForward("cartList");
}
return mapping.findForward("commitOrder");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?