删除购物车列表页面商品
一、实现思路
1、从前台页面传进来itemId。
2、从cookie中取所有商品的列表。
3、删除itemId对应的商品。
4、将商品列表再传回cookie中,并响应给浏览器。
二、jsp页面删除代码
1、请求url : /cart/delete/${cart.id}.html
<div class="cell p-remove"><a id="remove-11345721-1" data-more="removed-87.20-1" clstag="clickcart|keycount|xincart|btndel318558" class="cart-remove" href="/cart/delete/${cart.id}.html">删除</a>
三、service层代码
@Service public class CartServiceImpl implements CartService { @Autowired private ItemService itemService; @Value("${COOKIE_EXPIRE}") private Integer COOKIE_EXPIRE; ...... @Override public TaotaoResult deleteCartItem(Long itemId, HttpServletRequest request, HttpServletResponse response) { List<CartItem> lists = getCartLists(request); for (CartItem c : lists) { if(c.getId().longValue() == itemId) { lists.remove(c); break; } } CookieUtils.setCookie(request, response,"TT_CART",JsonUtils.objectToJson(lists), COOKIE_EXPIRE, true); return TaotaoResult.ok(); } }
四、controller层代码,重定向到cart.jsp页面。
@Controller public class CartController { @Autowired private CartService cartService;
....... @RequestMapping("/cart/cart") public String showCart(HttpServletRequest request, Model model) { List<CartItem> cartLists = cartService.getCartLists(request); model.addAttribute("cartList", cartLists); return "cart"; } ....... @RequestMapping("/cart/delete/{itemId}") public String deleteCartItem(@PathVariable Long itemId,HttpServletRequest request,HttpServletResponse response) { cartService.deleteCartItem(itemId, request, response); return "redirect:/cart/cart.html"; } }