image.png
/**
* 更新购物车商品的数量
*/
@RequestMapping("/cart/update/num/{itemId}/{num}")
@ResponseBody
public E3Result updateCatNum(@PathVariable Long itemId,@PathVariable Integer num,
HttpServletRequest request,HttpServletResponse response){
//1.取出购物车的列表
List<TbItem> itemList = getCartListFromCookie(request);
//2.遍历列表,找到对应的商品
for(TbItem item :itemList){
if(item.getId().longValue() == itemId){
//3.更新数量
item.setNum(num);
break;
}
}
//4.把购物车列表写回Cookie
CookieUtils.setCookie(request, response,"cart",JsonUtils.objectToJson(itemList),COOKIE_CART_EXPIRE,true);
//5.返回成功
return E3Result.ok();
}
image.png
网友评论