I have some UI where an admin can update products. During my dev/testing, I only ever opened one window, and everything worked as it should.
Then the client was editing, and they opened multiple tabs for different products, and upon saving, this caused a duplicate field issue.
I am assuming this is a combination of @SessionAttributes
and @ModelAttribute
. The last product opened, is the one put in the session, so if you try edit the first tab, you will actually have the incorrect product.
Is my approach below, using SessionAttribute
, and ModelAttribute
incorrect?
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();
}
}