java 在控制器中处理完@ModelAttribute 后,如何在 Spring MVC 中重置它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6877009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reset a @ModelAttribute in Spring MVC after it has been processed in the controller?
提问by HHWilchevsky
I have defined a @ModelAttribute("mymodel")
我已经定义了一个 @ModelAttribute("mymodel")
@ModelAttribute("mymodel")
MyModel mymodel() {
MyModel mymodel = new MyModel();
return mymodel;
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public final void save(@ModelAttribute("mymodel") MyModel mymodel,
final BindingResult binding,
final HttpServletRequest request,
final ModelMap modelMap) throws Exception {
modelService.save(mymodel);
// try to reset the model --> doesn't work!!!
myModel = new MyModel();
}
The problem is, even though I reset the model in the save method, if I reload the page after a save operation and save a second time, the model contains all of the values of the previous myModel
.
问题是,即使我在 save 方法中重置了模型,如果我在保存操作后重新加载页面并再次保存,该模型将包含前一个myModel
.
How do I reset it after it has been processed?
处理后如何重置?
回答by Russell
Unless I miss my guess, this is because
除非我错过了我的猜测,这是因为
myModel = new MyModel();
myModel = new MyModel();
is only going to reset the reference within the method, in the same way that getting a MyModel
from a List<MyModel>
and then calling myModel = new MyModel();
would not change the element in the List, only your local reference.
只会重置方法中的引用,就像MyModel
从 a获取 aList<MyModel>
然后调用myModel = new MyModel();
不会更改 List 中的元素一样,只会更改您的本地引用。
You most likely need to put the new MyModel() into the model or modelMap.
您很可能需要将新的 MyModel() 放入模型或模型映射中。
The redirect after post pattern may also be useful to you here. Have your POST method
帖子后重定向模式在这里也可能对您有用。有你的 POST 方法
return "redirect:originalpage.htm"
return "redirect:originalpage.htm"
This should reload the original page fresh, and will also mean that if you hit refresh you won't resubmit the POST, saving your object twice.
这应该重新加载原始页面,并且还意味着如果您点击刷新,您将不会重新提交 POST,将您的对象保存两次。
回答by blong824
I wouldn't do it like this.
我不会这样做的。
Try setting your model in the get request like :
尝试在 get 请求中设置您的模型,例如:
@RequestMapping(value = "/save", method = RequestMethod.GET)
public ModelAndView getSaveForm(ModelMap model) {
model.addAttribute("mymodel", new MyModel());
return new ModelAndView("newView", model);
}
回答by Justin Wrobel
I was running into a similar same issue while messing with the BookCatalog from Portlets in Action. My solution was to manually reset it using Model.addAttribute(). For example:
我在处理Portlets in Action 中的 BookCatalog 时遇到了类似的问题。我的解决方案是使用 Model.addAttribute() 手动重置它。例如:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public final void save(@ModelAttribute("mymodel") MyModel mymodel,
final BindingResult binding,
Model model
final HttpServletRequest request,
final ModelMap modelMap) throws Exception
{
modelService.save(mymodel);
model.addAttrubute("mymodel", new MyModel());
}