📄 addtocartaction.java
字号:
/*
* @author : Neelesh
* @Version : 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the File : AddToCartAction.java
* Creation/Modification History :
*
* Neelesh 06-Apr-2003 Created
*
*/
package oracle.otnsamples.vsm.actions.mall;
// IO and servlet 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;
/**
* This Action class adds an item to the cart. If there is no cart, a new one
* is created and added to the session.
*
* @author Neelesh
* @version 1.0
*/
public class AddToCartAction 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 there is no cart in the session, create a new one
if(request.getSession().getAttribute("_cart") != null) {
cart = (ShoppingCart) request.getSession().getAttribute("_cart");
} else {
cart = cartHome.create();
// We will store the EJB reference in Http session, since both
// web tier and EJB tier are colocated. If they are not, it is a good
// idea to store the Handle for the EJB in Http session.
request.getSession().setAttribute("_cart", cart);
// set the cart's locale to the request locale
// this remains the same until the user checks out the cart
cart.setUserLocale(getLocale(request));
}
int qty = Integer.parseInt(request.getParameter("qty"));
if(qty > 0) {
cart.addToCart(request.getParameter("itemID"), qty);
}
} catch(ServiceException ex) {
// handle errors
servlet.log("CartError", ex);
ActionErrors errors = new ActionErrors();
errors.add(
"CartError",
new ActionError(
"error.addtocart",
MessageCache.getMessage(
ex.getMessageCode(),
getLocale(request))));
saveErrors(request, errors);
} catch(Exception ex) {
// handle errors
servlet.log("CartError", ex);
ActionErrors errors = new ActionErrors();
errors.add("CartError", new ActionError("error.addtocart"));
saveErrors(request, errors);
}
// This is the query string in the originating page, needed to go back to
//the same page
request.setAttribute("query", request.getQueryString());
return mapping.findForward("success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -