How to reset a @ModelAttribute in Spring MVC after it has been processed in the controller?
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,
开发者_StackOverflow中文版 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?
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 a List<MyModel>
and then calling myModel = 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.
I wouldn't do it like this.
Try setting your model in the get request like :
@RequestMapping(value = "/save", method = RequestMethod.GET)
public ModelAndView getSaveForm(ModelMap model) {
model.addAttribute("mymodel", new MyModel());
return new ModelAndView("newView", model);
}
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:
@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());
}
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 {
try
{
// All code related to processing
}
catch(Exception e)
{
throw e;
}
//Just before sending back the response add below line
response.setHeader("Response_Key","Response_Header_value");
return PojoRS;
}
精彩评论