FileDocCategorySizeDatePackage
UpdateCartQuantitiesController.javaAPI DocExample1319Thu Dec 04 08:19:26 GMT 2003org.springframework.samples.jpetstore.web.spring

UpdateCartQuantitiesController

public class UpdateCartQuantitiesController extends Object implements org.springframework.web.servlet.mvc.Controller
author
Juergen Hoeller
since
30.11.2003

Fields Summary
Constructors Summary
Methods Summary
public org.springframework.web.servlet.ModelAndViewhandleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

		Cart cart = (Cart) WebUtils.getOrCreateSessionAttribute(request, "sessionCart", Cart.class);
		Iterator cartItems = cart.getAllCartItems();
		while (cartItems.hasNext()) {
			CartItem cartItem = (CartItem) cartItems.next();
			String itemId = cartItem.getItem().getItemId();
			try {
				int quantity = Integer.parseInt(request.getParameter(itemId));
				cart.setQuantityByItemId(itemId, quantity);
				if (quantity < 1) {
					cartItems.remove();
				}
			}
			catch (NumberFormatException e) {
				//ignore on purpose
			}
		}
		return new ModelAndView("Cart", "cart", cart);