public ActionForward CartItmeAction(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm dynaFrom = (DynaActionForm) form;
// 食物的dbo
Food f = (Food) dynaFrom.get("Food");
FoodBiz foodbiz = new FoodBiz();
// 购物车Map
Food food = foodbiz.getFoodByFoodId(f.getFoodId());
HttpSession session = request.getSession();
Map cartMap = (Map) session.getAttribute("cartMap");
// 如果没有创建购物车 就创建购物车
if (cartMap == null) {
cartMap = new HashMap();
// 将购物车存到Session
session.setAttribute("cartMap", cartMap);
}
// 获得购物车对象
CartItemEntity cartentity = (CartItemEntity) cartMap.get(food
.getFoodId());
if (cartentity != null) {
cartentity.setNum(cartentity.getNum() + 1);
} else {
cartMap.put(food.getFoodId(), new CartItemEntity(food, 1));
}
//迭代存到List
Collection coll = cartMap.values();
List list = new ArrayList();
for (Iterator iter = coll.iterator(); iter.hasNext();) {
CartItemEntity element = (CartItemEntity) iter.next();
list.add(element);
}
request.setAttribute("cartList", list);
return mapping.findForward("cartItem");
}