简体   繁体   English

如何使用 boolean 方法从购物车中删除 object?

[英]how i can remove object from shopping cart with boolean method?

i have problem.我有问题。 i have project to do and i have to make boolean method for remove book from the cart我有项目要做,我必须使用 boolean 方法从购物车中移除书籍

i tried this我试过这个

in my cart model在我的购物车中 model

    public boolean supprimer(String isbn)
    {

        List<LivreAchete> listelivre = (List<LivreAchete>) request.getSession().getAttribute("panier");
        listelivre = this.getListe();

        if(listelivre.removeIf((e)->e.getIsbn().equals(livre.getIsbn())))
        {

            request.getSession().setAttribute("panier",listelivre);
            return true;

        }
        else
            return false;   

    }

and in my controller在我的 controller

@RequestMapping(value="/librairie/supprimerLivre/{isbn}", method = RequestMethod.GET)
public String supprimerLivre(@PathVariable("isbn") String isbn, HttpServletRequest request){
    try{
     gestPanier = new GestPanier(request);
      //rechercher le livre qui correspond a l'isbn passer en parametre
     //LivreAchete livre = gestPanier.getListe().stream().filter(c -> c.getIsbn().equals(isbn)).findFirst().get();
    //supprimer le livre
    gestPanier.supprimer(isbn);
    return "redirect:/librairie/afficherPanier";  
    }
    catch(Exception ex){
        return "redirect:/librairie/"Error"
    }
}

but when i run my project and i try to delete book its dont work can someone help me to find my mistake please?但是当我运行我的项目并尝试删除这本书时,它不起作用,有人可以帮我找出我的错误吗?

It's kind of hard to see what is going on since we cannot see the rest of your classes, but my first impression is that this code here does not make sense:很难看到发生了什么,因为我们看不到您的课程的 rest,但我的第一印象是这里的代码没有意义:

List<LivreAchete> listelivre = (List<LivreAchete>)request.getSession().getAttribute("panier");
listelivre = this.getListe();

You are initializing a reference of List of LivreAchete to the session attribute, and then directly after that you are setting this newly declared listelivre to the return value of getList().您正在初始化 LivreAchete 列表对 session 属性的引用,然后直接将这个新声明的 listelivre 设置为 getList() 的返回值。

I would take a closer look at this section of code, but it's hard for me to tell what your intention is with this code.我会仔细看看这部分代码,但我很难说出你对这段代码的意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM