Spring MVC + Session属性和多个标签 [英] Spring MVC + Session attributes and multiple tabs

查看:321
本文介绍了Spring MVC + Session属性和多个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些管理员可以更新产品的UI。在我的开发/测试期间,我只打开了一个窗口,一切正常。

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.

我假设这是 @SessionAttributes 的组合, @ModelAttribute 。打开的最后一个产品是放入会话的产品,所以如果你尝试编辑第一个标签,你实际上会有不正确的产品。

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.

我的方法是否在下面,使用 SessionAttribute ModelAttribute 不正确?

Is my approach below, using SessionAttribute, and ModelAttribute incorrect?

我的控制器:

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

}

推荐答案

我最终使用自定义SessionAttributeStore,基于Marty Jones的文章

I ended up using a custom SessionAttributeStore, based on the article by Marty Jones

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

这篇关于Spring MVC + Session属性和多个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆