【发布时间】:2015-11-17 03:47:07
【问题描述】:
我在 spring mvc 中创建购物车。我可以使用以下功能添加购物车并添加继续购物车:
@RequestMapping(value = "/ordernow", method = RequestMethod.POST)
public String ordernow(@ModelAttribute("product") Product product, HttpSession session) {
if (session.getAttribute("cart") == null) {
List<Item> cart = new ArrayList<Item>();
cart.add(new Item(this.pm.find(product.getId()), 1));
session.setAttribute("cart", cart);
} else {
List<Item> cart = (List<Item>) session.getAttribute("cart");
boolean flag = false;
for (Item item : cart) {
if (item.getProduct().getId() == product.getId()) {
item.setQuantity(item.getQuantity() + 1);
flag = true;
break;
}
}
if (flag == false) {
cart.add(new Item(this.pm.find(product.getId()), 1));
}
}
return "cart";
}
当我刷新页面数量总是增加。我不想在刷新页面时增加数量。我想要增加数量,除非我点击按钮 AddToCart。 请帮帮我!
【问题讨论】:
标签: java hibernate spring-mvc