Java Code Examples for org.springframework.web.util.WebUtils#getOrCreateSessionAttribute()

The following examples show how to use org.springframework.web.util.WebUtils#getOrCreateSessionAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ViewCartController.java    From jpetstore-kubernetes with Apache License 2.0 6 votes vote down vote up
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
	Cart cart = (Cart) WebUtils.getOrCreateSessionAttribute(request.getSession(), "sessionCart", Cart.class);
	String page = request.getParameter("page");
	if (userSession != null) {
		if ("next".equals(page)) {
			userSession.getMyList().nextPage();
		}
		else if ("previous".equals(page)) {
			userSession.getMyList().previousPage();
		}
	}
	if ("nextCart".equals(page)) {
		cart.getCartItemList().nextPage();
	}
	else if ("previousCart".equals(page)) {
		cart.getCartItemList().previousPage();
	}
	return new ModelAndView(this.successView, "cart", cart);
}
 
Example 2
Source File: UpdateCartQuantitiesController.java    From jpetstore-kubernetes with Apache License 2.0 6 votes vote down vote up
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	Cart cart = (Cart) WebUtils.getOrCreateSessionAttribute(request.getSession(), "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 ex) {
			// ignore on purpose
		}
	}
	request.getSession().setAttribute("sessionCart", cart);
	return new ModelAndView("Cart", "cart", cart);
}
 
Example 3
Source File: AddItemToCartController.java    From jpetstore-kubernetes with Apache License 2.0 6 votes vote down vote up
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	Cart cart = (Cart) WebUtils.getOrCreateSessionAttribute(request.getSession(), "sessionCart", Cart.class);
	String workingItemId = request.getParameter("workingItemId");
	if (cart.containsItemId(workingItemId)) {
		cart.incrementQuantityByItemId(workingItemId);
	}
	else {
		// isInStock is a "real-time" property that must be updated
		// every time an item is added to the cart, even if other
		// item details are cached.
		boolean isInStock = this.petStore.isItemInStock(workingItemId);
		Item item = this.petStore.getItem(workingItemId);
		cart.addItem(item, isInStock);
	}
	return new ModelAndView("Cart", "cart", cart);
}
 
Example 4
Source File: ViewCartController.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
	Cart cart = (Cart) WebUtils.getOrCreateSessionAttribute(request.getSession(), "sessionCart", Cart.class);
	String page = request.getParameter("page");
	if (userSession != null) {
		if ("next".equals(page)) {
			userSession.getMyList().nextPage();
		}
		else if ("previous".equals(page)) {
			userSession.getMyList().previousPage();
		}
	}
	if ("nextCart".equals(page)) {
		cart.getCartItemList().nextPage();
	}
	else if ("previousCart".equals(page)) {
		cart.getCartItemList().previousPage();
	}
	return new ModelAndView(this.successView, "cart", cart);
}
 
Example 5
Source File: UpdateCartQuantitiesController.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	Cart cart = (Cart) WebUtils.getOrCreateSessionAttribute(request.getSession(), "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 ex) {
			// ignore on purpose
		}
	}
	request.getSession().setAttribute("sessionCart", cart);
	return new ModelAndView("Cart", "cart", cart);
}
 
Example 6
Source File: AddItemToCartController.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	Cart cart = (Cart) WebUtils.getOrCreateSessionAttribute(request.getSession(), "sessionCart", Cart.class);
	String workingItemId = request.getParameter("workingItemId");
	if (cart.containsItemId(workingItemId)) {
		cart.incrementQuantityByItemId(workingItemId);
	}
	else {
		// isInStock is a "real-time" property that must be updated
		// every time an item is added to the cart, even if other
		// item details are cached.
		boolean isInStock = this.petStore.isItemInStock(workingItemId);
		Item item = this.petStore.getItem(workingItemId);
		cart.addItem(item, isInStock);
	}
	return new ModelAndView("Cart", "cart", cart);
}
 
Example 7
Source File: RemoveItemFromCartController.java    From jpetstore-kubernetes with Apache License 2.0 4 votes vote down vote up
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	Cart cart = (Cart) WebUtils.getOrCreateSessionAttribute(request.getSession(), "sessionCart", Cart.class);
	cart.removeItemById(request.getParameter("workingItemId"));
	return new ModelAndView("Cart", "cart", cart);
}
 
Example 8
Source File: RemoveItemFromCartController.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	Cart cart = (Cart) WebUtils.getOrCreateSessionAttribute(request.getSession(), "sessionCart", Cart.class);
	cart.removeItemById(request.getParameter("workingItemId"));
	return new ModelAndView("Cart", "cart", cart);
}