📄 processcheckoutaction.java
字号:
package netstore.order;
import java.util.Map;
import java.util.HashMap;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import netstore.framework.exceptions.*;
import netstore.framework.SessionContainer;
import netstore.framework.ShoppingCartItem;
import netstore.framework.ShoppingCart;
import netstore.framework.NetstoreBaseAction;
import netstore.service.*;
import netstore.framework.NetstoreLookupDispatchAction;
import netstore.businessobjects.*;
import java.util.List;
import java.util.Iterator;
import org.apache.struts.util.MessageResources;
import netstore.framework.util.IConstants;
public class ProcessCheckoutAction extends NetstoreLookupDispatchAction {
//根据key值,决定调用函数
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.checkout", "checkout");
map.put("button.saveorder", "saveorder");
return map;
}
public ActionForward saveorder(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
//do save order
saveOrder(request);
return mapping.findForward(IConstants.SUCCESS_KEY);
}
public ActionForward checkout(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
//do check out
saveOrder(request);
return mapping.findForward(IConstants.SUCCESS_KEY);
}
public void saveOrder(HttpServletRequest request) throws IOException,
ServletException {
try {
//save order
OrderServiceImpl serviceImpl = getOrderService();
SessionContainer sessionContainer = getSessionContainer(request);
ShoppingCart cart = sessionContainer.getCart();
Customer customer = sessionContainer.getCustomer();
Order order = new Order();
order.setCustomer(customer);
//随机生成一个订单编号
order.setOrderNumber(new Double(Math.random()
* System.currentTimeMillis()).toString().substring(3, 8));
List items = cart.getItems();
Iterator it = items.iterator();
while (it.hasNext()) {
ShoppingCartItem cartItem = (ShoppingCartItem) it.next();
//数量,单价,订单,产品
LineItem lineItem = new LineItem(cartItem.getQuantity(),
cartItem.getBasePrice().doubleValue(), order, cartItem
.getItem());
order.getLineItems().add(lineItem);
}
//这样就能保存订单了
serviceImpl.saveOrder(order);
sessionContainer.setCurrentOrderNumber(order.getOrderNumber());
} catch (DatastoreException ex) {
throw new ServletException(ex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -