shoppingcartactions.java
来自「《基于Eclipse的开源框架技术与实战》[第5章]随书源码」· Java 代码 · 共 137 行
JAVA
137 行
package com.free.struts.storefront.order;
import java.io.IOException;
import java.text.Format;
import java.text.NumberFormat;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;
import com.free.struts.storefront.catalog.view.ItemDetailView;
import com.free.struts.storefront.framework.ShoppingCart;
import com.free.struts.storefront.framework.ShoppingCartItem;
import com.free.struts.storefront.framework.StorefrontDispatchAction;
import com.free.struts.storefront.framework.UserContainer;
import com.free.struts.storefront.framework.util.IConstants;
import com.free.struts.storefront.service.IStorefrontService;
/**
* <p>Title: Eclipse Plugin Development</p>
* <p>Description: Free download</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: Free</p>
* @author gan.shu.man
* @version 1.0
*/
public class ShoppingCartActions extends StorefrontDispatchAction {
/**
* 查看当前购物车的状态,转到shoppingcart.jsp面面
*/
public ActionForward view(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
//确保存UserContainer已经创建
UserContainer userContainer = getUserContainer(request);
return mapping.findForward(IConstants.SUCCESS_KEY);
}
/**
* 更新购物车中的商品以及数量
*/
public ActionForward update(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
updateItems(request);
updateQuantities(request);
return mapping.findForward(IConstants.SUCCESS_KEY);
}
/**
* 添加商品到购物车
*/
public ActionForward addItem(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
System.out.println( "IN add item" );
UserContainer userContainer = getUserContainer(request);
System.out.println( "user container is " + userContainer );
System.out.println( "Begin" );
System.out.println( "Looking: " + request.getSession().getAttribute( IConstants.USER_CONTAINER_KEY ));
System.out.println( "End" );
//获得商品的ID
String itemId = request.getParameter( IConstants.ID_KEY );
String qtyParameter = request.getParameter( IConstants.QTY_KEY );
int quantity = 1; // Default value to add
if(qtyParameter != null) {
Locale userLocale = userContainer.getLocale();
Format nbrFormat = NumberFormat.getNumberInstance(userLocale);
try {
Object obj = nbrFormat.parseObject(qtyParameter);
quantity = ((Number)obj).intValue();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
IStorefrontService serviceImpl = getStorefrontService();
ItemDetailView itemDetailView = serviceImpl.getItemDetailView( itemId );
//添加商品到购物车中
userContainer.getCart().addItem(new ShoppingCartItem(itemDetailView, quantity));
return mapping.findForward(IConstants.SUCCESS_KEY);
}
/**
* 更新购物车中的商品,
* 当前只有删除商品时会更新购物车中商品的数量
*/
private void updateItems(HttpServletRequest request) {
// Multiple checkboxes with the name "deleteCartItem" are on the
// form. The ones that were checked are passed in the request.
String[] deleteIds = request.getParameterValues("deleteCartItem");
// Build a List of item ids to delete
if(deleteIds != null && deleteIds.length > 0) {
int size = deleteIds.length;
List itemIds = new ArrayList();
for(int i = 0;i < size;i++) {
itemIds.add(deleteIds[i]);
}
//删除购物车中的商品
UserContainer userContainer = getUserContainer(request);
userContainer.getCart().removeItems(itemIds);
}
}
/**
* 更新购物车中商品的数量
*/
private void updateQuantities(HttpServletRequest request) {
Enumeration enumType = request.getParameterNames();
while(enumType.hasMoreElements()) {
String paramName = (String)enumType.nextElement();
if(paramName.startsWith("qty_")) {
String id = paramName.substring(4, paramName.length());
String qtyStr = request.getParameter(paramName);
if(id != null && qtyStr != null) {
ShoppingCart cart = getUserContainer(request).getCart();
cart.updateQuantity(id, Integer.parseInt(qtyStr));
}
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?