📄 updatecartaction.java
字号:
/*
* @author : Neelesh
* @Version : 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the File : UpdateCartAction.java
* Creation/Modification History :
*
* Neelesh 14-Jan-2003 Created
*
*/
package oracle.otnsamples.vsm.actions.mall;
// Servlet and IO API
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import oracle.otnsamples.util.MessageCache;
import oracle.otnsamples.util.ServiceLocator;
import oracle.otnsamples.vsm.services.ServiceException;
import oracle.otnsamples.vsm.services.ShoppingCart;
import oracle.otnsamples.vsm.services.ShoppingCartHome;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* The action class updates the cart with new quantities entered and redirects
* to the originating page
*
* @author Neelesh
* @version 1.0
*/
public class UpdateCartAction extends Action {
/**
* This is the action called from the Struts framework to
* add an item to user cart.
*
* @param <b>mapping</b> The ActionMapping used to select this instance.
* @param <b>form</b> The optional ActionForm bean for this request.
* @param <b>request</b> The HTTP Request we are processing.
* @param <b>response</b> The HTTP Response we are processing.
*
* @return <b>ActionForward</b> The control forward information
*
* @throws <b>IOException</b>
* @throws <b>ServletException</b>
*/
public ActionForward execute(
ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
ShoppingCartHome cartHome =
(ShoppingCartHome) ServiceLocator.getLocator().getService("ShoppingCart");
ShoppingCart cart = null;
try {
if(request.getSession().getAttribute("_cart") != null) {
cart = (ShoppingCart) request.getSession().getAttribute("_cart");
String key = null;
// get item ids in the cart
String[] items = request.getParameterValues("items");
if(items != null) {
// get new quantities
int len = items.length;
int[] qty = new int[ len ];
for(int i = 0; i < len; i++) {
qty [ i ] =
Integer.parseInt(request.getParameter("quantity" + items [ i ]));
}
cart.updateCart(items, qty);
}
}
} catch(ServiceException ex) {
// handle errors
servlet.log("UpdateCartError", ex);
ActionErrors errors = new ActionErrors();
errors.add(
"CartError",
new ActionError(
"error.updatecart",
MessageCache.getMessage(
ex.getMessageCode(),
getLocale(request))));
saveErrors(request, errors);
} catch(Exception ex) {
// handle errors
servlet.log("UpdateCartError", ex);
ActionErrors errors = new ActionErrors();
errors.add("CartError", new ActionError("error.updatecart"));
saveErrors(request, errors);
}
request.setAttribute("query", request.getParameter("query"));
return mapping.findForward("success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -