📄 shopcartaction.java
字号:
package org.y2t12.struts.action;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.beanutils.BeanUtils;
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.y2t12.beans.ShopBean;
import org.y2t12.service.ShopService;
import org.y2t12.struts.form.ShopCartForm;
public class ShopCartAction extends DispatchAction {
/**
* 向购物车中添加商品
*
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward addToCart(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ShopCartForm formBean = (ShopCartForm) form;
ShopBean bean = new ShopBean();
try {
BeanUtils.copyProperties(bean, formBean);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
String gotoURL = "";
HttpSession session = request.getSession();
ShopService service = (ShopService) session.getAttribute("book");
if (null == service) {
service = new ShopService();
}
if (service.isExist(bean)) {
service.addShopCart(bean);
session.setAttribute("book", service);
gotoURL = "noexist";
} else {
gotoURL = "exist";
}
return mapping.findForward(gotoURL);
}
/**
* 察看购物车
*
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward toViewCart(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String gotoURL = "no";
HttpSession session = request.getSession();
ShopService shopcart = (ShopService) session.getAttribute("book");
if (null != shopcart) {
Collection coll = shopcart.getAll();
session.setAttribute("list", coll);
session.setAttribute("book", shopcart);
request.setAttribute("total", new Double(shopcart.getTotal()));
gotoURL = "yes";
}
return mapping.findForward(gotoURL);
}
/**
* 删除购物车中的物品
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward deleteBook(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
int bookID = Integer.parseInt(request.getParameter("id"));
System.out.println(bookID);
HttpSession session = request.getSession();
ShopService shopcart = (ShopService) session.getAttribute("book");
shopcart.removeBook(new Integer(bookID));
Collection coll = shopcart.getAll();
request.setAttribute("total", new Double(shopcart.getTotal()));
session.setAttribute("list", coll);
return mapping.findForward("yes");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -