Spring MVC + session attributes and multiple tabs

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(); } 

}

+4
source share
2 answers

I ended up using my own SessionAttributeStore, based on an article by Marty Jones

http://marty-java-dev.blogspot.com/2010/09/spring-3-session-level-model-attributes.html

+2
source

In cases where you simply show the object stored in the session and do not allow it to be edited or replaced, this approach is fine. But for such cases, it is advisable to use the request area rather than the session area.

+1
source

Source: https://habr.com/ru/post/1414876/


All Articles