I have some interface where the administrator can update products. During my dev / testing, I only ever opened a single window, and everything worked as it should.
Then the client edited, and they opened several tabs for different products, and when saving this caused a problem with a duplicate field.
I assume this is a combination of @SessionAttributes
and @ModelAttribute
. The last product open is the one that fits into the session, so if you try to edit the first tab, you really will have the wrong product.
Is my approach below using SessionAttribute
and ModelAttribute
wrong?
My controller:
@Controller @SessionAttributes({ "product" }) public class ProductController { @RequestMapping(value = "/product/update/{productId}", method = RequestMethod.GET) public String update(@PathVariable Long productId, Model model) { Product product; if (productId == null) { product = new Product(); } else { product = Product.find(productId); } model.addAttribute("product", product); return "product/update"; } @RequestMapping(value = "/product/update", method = RequestMethod.POST) public String update(@ModelAttribute Product product, BindingResult result, Model model) { if (result.hasErrors()) { return "product/update"; } product = product.merge(); return "redirect:/product/update/" + product.getId(); }
}
kabal source share