在控制器中处理完 @ModelAttribute 后,如何在 Spring MVC 中重置 @ModelAttribute?
我定义了一个 @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();
}
问题是,即使我在保存方法中重置模型,如果我在保存操作后重新加载页面并再次保存,该模型包含的所有值之前的myModel
。
处理完成后如何重置?
I have defined a @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
.
How do I reset it after it has been processed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除非我错过了我的猜测,否则这是因为
myModel = new MyModel();
只会重置方法内的引用,就像从中获取
MyModel
一样List
然后调用myModel = new MyModel();
不会更改列表中的元素,只会更改本地引用。您很可能需要将新的 MyModel() 放入模型或 modelMap 中。
发布后重定向模式可能对您也有用。让您的 POST 方法
返回“redirect:originalpage.htm”
这应该重新加载原始页面,并且还意味着如果您点击刷新,您将不会重新提交 POST,从而保存您的对象两次。
Unless I miss my guess, this is because
myModel = new MyModel();
is only going to reset the reference within the method, in the same way that getting a
MyModel
from aList<MyModel>
and then callingmyModel = new MyModel();
would not change the element in the List, only your local reference.You most likely need to put the new MyModel() into the model or modelMap.
The redirect after post pattern may also be useful to you here. Have your POST method
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.
我不会这样做。
尝试在 get 请求中设置您的模型,例如:
I wouldn't do it like this.
Try setting your model in the get request like :
我在处理 Portlet in Action 中的 BookCatalog 时遇到了类似的问题。我的解决方案是使用 Model.addAttribute() 手动重置它。例如:
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:
当控制器完成处理后我们想要添加响应标头时,下面的示例方法有效。 @ModelAttribute("mymodel") 在控制器方法处理之前调用,因此我们不能根据处理结果放置任何值。
//在请求方法中添加一个参数“HttpServletResponse”
@RequestMapping(method = RequestMethod.POST, value = APIConstants.Request_URL, headers = APIConstants.Request_HEADER, Produces = APIConstants.PRODUCES)
公共@ResponseBody PojoRS autheticateByPassword(@RequestBody()PojoRQ apiRequest,HttpServletResponse响应)
抛出异常{
Below sample method works when we want to add the response header once the controller has finished its processing. @ModelAttribute("mymodel") is invoked before controller method processing so we can not put any values based on processing results.
//Add a parameter "HttpServletResponse"in the request method
@RequestMapping(method = RequestMethod.POST, value = APIConstants.Request_URL, headers = APIConstants.Request_HEADER, produces = APIConstants.PRODUCES)
public @ResponseBody PojoRS autheticateByPassword(@RequestBody() PojoRQ apiRequest, HttpServletResponse response)
throws Exception {