Reputation: 943
Let's say I have a web form and whenever the form is submitted it undergoes validation.
If there is an error I redirect it to the same page and uses RedirectAttributes to maintain the form values for resubmission. Else, it submits to the next page successfully.
Problem: (1) Whenever I go back, or revisit the same form url, it always retains the submitted inputs (2) Trying to submit new values, it changes (updates) the previous data submitted. (3) After a successful submission I want it to be cleared as a new form object.
So far here is my Controller Code:
GET
@RequestMapping (method = RequestMethod.GET)
public String setForm(Model model, HttpSession session){
Object o;
if(!model.containsAttribute("object")){
model.addAttribute("object", new Object());
}
else{
l= (Object)modelasMap().get("object");
}
return "app/formUrl";
}
While in my POST
@RequestMapping(method = RequestMethod.POST)
public String formSubmit(@ModelAttribute("object" Object object,
final BindingResult result, HttpSession session,
SessionStatus status, RedirectAttributes attr){
//DO some object manipulation code here
//validation code
if(result.hasErrors()){
attr.addFlashAttribute(org.springframework.validation.BindingResult.object", result);
attr.addFlashAttribute("object", object);
return "redirect:/formUrl";
}
else{
//save form inputs here
}
}
return "redirect:successPage";
}
I'm really confused as to why its not returning a new cleared form when I have set a condition if(!model.containsAttribute("object"))
in my GET.
I hope someone could explain to me what seems to be the problem. Thanks in advance!
EDIT:
Adding <mvc:annotation-driven ignore-default-model-on-redirect = "true"/>
gives me the error:
cvc-complex-type.3.2.2: Attribute 'ignoreDefaultModelOnRedirect' is not allowed to appear in element 'mvc:annotation-driven'.
Same error if I use ignoreDefaultModelOnRedirect
Additional information - I am on Maven build.
Upvotes: 2
Views: 2552
Reputation: 1173
Have you tried setting ignore-default-model-on-redirect
attribute to true in your servlet configuration? As follows:
<mvc:annotation-driven ignore-default-model-on-redirect="true" />
From the documentation:
By default, the content of the "default" model is used both during rendering and redirect scenarios. Alternatively a controller method can declare a RedirectAttributes argument and use it to provide attributes for a redirect. Setting this flag to true ensures the "default" model is never used in a redirect scenario even if a RedirectAttributes argument is not declared. Setting it to false means the "default" model may be used in a redirect if the controller method doesn't declare a RedirectAttributes argument. The default setting is false but new applications should consider setting it to true.
Upvotes: 3
Reputation: 943
So found the answer. I replaced the whole GET method with a ModelAndView with respective parameters (url, model, new Object()).
And under 'if(result.hasErrors())' return should be a forwarding not a redirection url.
Upvotes: 0