📄 cartcheckoutaction.java
字号:
/*
* @author : Neelesh
* @Version : 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the File : CartCheckoutAction.java
* Creation/Modification History :
*
* Neelesh 14-Jan-2003 Created
*
*/
package oracle.otnsamples.vsm.actions.mall;
// Servlet and IP 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.vsm.actions.forms.ShippingAddressForm;
import oracle.otnsamples.vsm.services.ServiceException;
import oracle.otnsamples.vsm.services.ShoppingCart;
import oracle.otnsamples.vsm.services.data.Address;
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;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
* This Action class checks out the cart after updating the shipping address.
* The action also indicates any errors that may have occured during checkout.
* If the checkout action is successful, the cart bean is removed from the
* session.
*
* @author Neelesh
* @version 1.0
*/
public class CartCheckoutAction extends HomePageAction {
/**
* This is the action called from the Struts framework to update the shipping
* address and checkout cart address for the current user so that the user
* can change it if needed.
*
* @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> - details of control forward.
*
* @throws <b>IOException</b>
* @throws <b>ServletException</b>
*/
public ActionForward execute(
ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
ShippingAddressForm addressForm = (ShippingAddressForm) form;
ActionErrors errors = new ActionErrors();
if(request.getSession().getAttribute("_cart") != null) {
ShoppingCart cart =
(ShoppingCart) request.getSession().getAttribute("_cart");
try {
// update shipping address
Address address = new Address();
address.setAddress(addressForm.getAddress());
address.setCity(addressForm.getCity());
address.setCountry(addressForm.getCountry());
address.setPhone(addressForm.getPhone());
address.setState(addressForm.getState());
address.setZip(addressForm.getZipAsInteger());
cart.changeShippingAddress(address);
// check out
String errorMessage = cart.checkout();
if(errorMessage != null) {
errors.add(
"CheckoutError",
new ActionError("error.cart.outofstock", errorMessage));
saveErrors(request, errors);
} else {
//remove the cart
ActionMessages messages = new ActionMessages();
messages.add(
"CheckoutSuccess",
new ActionMessage("cart.checkout.success"));
saveMessages(request, messages);
cart.remove();
request.getSession().removeAttribute("_cart");
}
} catch(ServiceException ex) {
// handle errors
servlet.log("CheckoutError", ex);
errors.add(
"CheckoutError",
new ActionError(
"error.displaycart",
MessageCache.getMessage(
ex.getMessageCode(),
getLocale(request))));
saveErrors(request, errors);
} catch(Exception ex) {
servlet.log("CheckoutError", ex);
errors.add("CheckoutError", new ActionError("error.displaycart"));
saveErrors(request, errors);
}
}
// call super class method to set navigation related data
super.execute(mapping, form, request, response);
return mapping.findForward("success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -