Java 请求之间的Spring-preserve-modelsattribute
在调用save之前,我尝试实现以下步骤的循环Java 请求之间的Spring-preserve-modelsattribute,java,spring,spring-mvc,session-variables,modelattribute,Java,Spring,Spring Mvc,Session Variables,Modelattribute,在调用save之前,我尝试实现以下步骤的循环 显示列表 添加到列表中 它可以添加一个项目。在添加的第二个项上,spring丢失了对已包含项的的引用,并尝试从表单数据重建它,但它不能这样做,因为它包含多态类型 如何在每次添加过程中保留更新的模型 样品 @RequestMapping(value=“/foo”,method=RequestMethod.GET) 公共字符串显示(@modeldattribute(“foos”)ArrayList foo,映射模型){ 模型.put(“foo”,foo
@RequestMapping(value=“/foo”,method=RequestMethod.GET)
公共字符串显示(@modeldattribute(“foos”)ArrayList foo,映射模型){
模型.put(“foo”,foo);
返回“foo.jsp”;
}
@RequestMapping(value=“/addFoo”,method=RequestMethod.POST)
公共字符串add(@modeldattribute(“foos”)ArrayList foo,
重定向属性(重定向属性){
add(新的FooImpl());
redirectAttributes.addFlashAttribute(“foos”,foo);
返回“重定向:foo”;
}
如果不使用,我怎么做?您基本上需要以某种方式使用会话属性。或者保存并检索对象。@SotiriosDelimanolis我很害怕。持久化和检索本质上模拟会话属性。我在想,也许一个、方面或其他一些机制可以证明是有用的。为什么不需要会话属性呢?此外,重定向属性也可以正常工作(它们也使用会话)。@SotiriosDelimanolis因为会话属性不容易在流中断时清除状态。重定向属性是我使用的不好的属性success@SotiriosDelimanolis在我的例子中,可以为两个用户打开两个选项卡,我不想要会话属性?
@RequestMapping(value = "/foo", method = RequestMethod.GET)
public String show(@ModelAttribute("foos") ArrayList<Foo> foo, Map model) {
model.put("foo", foo);
return "foo.jsp";
}
@RequestMapping(value = "/addFoo", method = RequestMethod.POST)
public String add(@ModelAttribute("foos") ArrayList<Foo> foo,
RedirectAttributes redirectAttributes) {
foo.add(new FooImpl());
redirectAttributes.addFlashAttribute("foos", foo);
return "redirect:foo";
}