✅P243_商城业务-购物车-RedirectAttribute
2024/2/14大约 1 分钟
目前问题
不断刷新“购物车” 页面,会不断发请求,数量会不断增长:

问题解决
解决方法:
- 以前商品添加到购物车会跳转到“购物车列表” 页面,现在我们修改成重定向到另一个接口,查询跳转到成功页面的数据
Controller
cfmall-cart/src/main/java/com/gyz/cfmall/controller/CartController.java
RedirectAttributes的addFlashAttribut()方法:将对象存储在Session中且只能使用一次,再次刷新就消失
RedirectAttributes的addAttribut()方法:将对象拼接在url中
    @GetMapping("/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId,
                              @RequestParam("num") Integer num,
                              Model model,
                              RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        CartItemVo cartItemVo = cartService.addCartItem(skuId, num);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.cfmall.com/addCartItmSuccess.html";
    }
    @GetMapping("/addCartItmSuccess.html")
    public String addCartItmSuccess(@RequestParam("skuId") Long skuId, Model model) {
        CartItemVo cartItemOne = cartService.getCartItem(skuId);
        model.addAttribute("cartItem", cartItemOne);
        return "success";
    }Service
cfmall-cart/src/main/java/com/gyz/cfmall/service/impl/CartServiceImpl.java
@Override
public CartItemVo getCartItem(Long skuId) {
    //拿到要操作的购物车信息
    BoundHashOperations<String, Object, Object> cartOps = getCartOpts();
    String redisValue = (String) cartOps.get(skuId.toString());
    CartItemVo cartItemVo = JSON.parseObject(redisValue, CartItemVo.class);
    return cartItemVo;
}success.html
cfmall-cart/src/main/resources/templates/success.html

<div th:if="${cartItem==null}" class="mc success-cont">
    <h2>购物车中无此商品</h2>
    <a href="http://cfmall.com/">去购物</a>
</div>效果演示
不断刷新购物车页面,数量始终不变
