不从购物车中删除项目。当我点击“删除”的项目仍然存在。只执行总价格计算。购物车不是存储库,而小说项目是一个存储库实体,保存在数据库中感谢您的帮助这是我的代码:
@Component
public class Cart {
private List <Fiction> fictions = new ArrayList<Fiction> ();
private float totale = 0.0f;
public Cart() {
super();
}
public void add(Fiction fiction) {
fictions.add(fiction);
this.totale = totale + fiction.getPrezzo();
}
public void remove(Fiction fiction) {
Iterator<Fiction> iterator = fictions.iterator();
while(iterator.hasNext()) {
Fiction item = iterator.next();
if(item.equals(fiction)) {
iterator.remove();
}
} this.totale = totale - fiction.getPrezzo();
}
控制器
Does not remove items from cart.
When I hit "delete" the items are still there.
Only performs the total price calculation.
Thanks for your help
This is my code :
@Controller
public class CartController {
@Autowired
Cart carrello;
@Autowired
private FictionRepo fictionRepo;
@RequestMapping("/deleteToCart/{id}")
public String deleteToCart(@PathVariable("id") Long id ) {
Fiction fictions = fictionRepo.findById(id).orElseThrow(() -> new IllegalArgumentException("Fiction non trovata"));
System.out.println(" sto per cancellare");
carrello.remove(fictions);
System.out.println(" ho cancellato");
return "redirect:/indexCart";
}
}
1条答案
按热度按时间htrmnn0y1#
重写
equals
以使代码正常工作。请参考以下示例: