簡體   English   中英

如何使用 boolean 方法從購物車中刪除 object?

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

我有問題。 我有項目要做,我必須使用 boolean 方法從購物車中移除書籍

我試過這個

在我的購物車中 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;   

    }

在我的 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"
    }
}

但是當我運行我的項目並嘗試刪除這本書時,它不起作用,有人可以幫我找出我的錯誤嗎?

很難看到發生了什么,因為我們看不到您的課程的 rest,但我的第一印象是這里的代碼沒有意義:

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

您正在初始化 LivreAchete 列表對 session 屬性的引用,然后直接將這個新聲明的 listelivre 設置為 getList() 的返回值。

我會仔細看看這部分代碼,但我很難說出你對這段代碼的意圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM