Hello, I am trying to implement a cart.
In Mapu save objects basket. After that, if the order is confirmed everything goes to the database.
Faced the problem that if the goods are added to the cart and enter the site from another computer, the added goods remain.
And actually the question is, how is it better to implement the basket?
Google has very little information on this issue, so I thought it was a bad practice to implement an online store or shopping cart on java?
Here is my code:
@Service("bookStoreService") public class BookStoreService { private Map<Product, Integer> products = new LinkedHashMap<>(); @Transactional public void addProductToBasket(Product product, int count) { products.put(product, count); } } Controller
@RequestMapping(value="/addProduct/{id}",method = RequestMethod.GET) public String addProduct(@PathVariable("id") Integer id){ Product product = bookService.getProductById(id); bookService.addProductToBasket(product, 1); return "redirect:/"; } @RequestMapping(value = "/") public String index(Model model,Model mode1) { model.addAttribute("listProduct",bookService.listPtoduct()); mode1.addAttribute("productInBasket", bookService.getBasket()); return "index"; } index.jsp
<c:if test="${productInBasket.size()>0}"> <table> <tr> <td>Название</td> <td>Цена</td> <td>Кол-во</td> <td><a href="/BookStore/clear">Очистить</a></td> </tr> <c:forEach items="${productInBasket}" var="qwe"> <tr> <td>${qwe.key.name}</td> <td>${qwe.key.price}</td> <td>${qwe.value}</td> </tr> </c:forEach> </table> </c:if>