Reputation: 8922
I m actually studying the Spring framework with Spring Boot and Sprint MVC. I m doing a form that post some data to complete an object, and I need to validate some values.
The fact is that the validation works, in fact, if I dont respect the validation, I m "redirected" (not really in fact, the URL doesn't change) to the error.html content.
How should I do to manage my redirection correctly ? This doesn't work this way :
@RequestMapping(value = "/print", method = RequestMethod.POST)
public String printPost(@ModelAttribute("printerentity") @Valid PrinterEntity print, Model model, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "redirect:/formPrinter";
}
model.addAttribute("printed", print.getName());
model.addAttribute("printerentity", new PrinterEntity());
return "index";
}
And the form :
<form method="post" th:action="@{/print}" th:object="${printerentity}">
<input type="text" th:field="*{name}"/>
<button type="submit">Valider</button>
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</p>
</form>
What am I doing wrong ?
EDIT : It seems that when I have an error, I dont pass in the controller code :o =>
@RequestMapping(value = "/print", method = RequestMethod.POST)
public String printPost(@ModelAttribute("printerentity") @Valid PrinterEntity print, Model model, BindingResult bindingResult) {
System.out.println("I dont passe here when error but I m redirected");
if (bindingResult.hasErrors()) {
return "formPrinter";
}
model.addAttribute("printed", print.getName());
model.addAttribute("printerentity", new PrinterEntity());
return "index";
}
thanks for advance
Upvotes: 0
Views: 2606
Reputation: 1
Just a little addon to imacd's comment about the parameters' order. As per Spring Boot Reference, BindingResult parameter should be added immediately next to @ModelAttribute in the parameters list of @RequestMapping method. Then Spring processes as expected and injects binding result to the model attribute instead of redirecting to the error page. So if by any chance you need to add other parameters (i.e. HttpServletRequest, Model or whatever) do it before @ModelAttribute or after BindingResult, but not between them.
Upvotes: 0
Reputation: 1
I had the same problem and it was solved by reordering the parameters in my @PostMapping method to (@Valid @ModelAttribute Form form, BindingResult bindingResult, Model model)
When the params were in the same order as your controller (@Valid @ModelAttribute Form form, Model model, BindingResult bindingResult) I was also redirected to /error without the controller @PostMethod being called.
Upvotes: 0
Reputation: 3748
How should I do to manage my redirection correctly ?
When you redirect a request, then the current request destroyed, and a new request object is created to process the request, so as per Sotirios Delimanolis has mentioned in comment, that model attributes are request attributes, which are available for per request only, If you want to store model attributes in the HTTP session between requests use @SessionAttributes, or a FlashAttributes.
As per your html form you have:
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</p>
you mean you want to show validation errors in the same page, then don't redirect return the same view.
if (bindingResult.hasErrors()) {
//return "redirect:/formPrinter";
return "your html form view";
}
then, on your view you can render all validation error messages like:
<p th:each="err : ${#fields.errors('*')}" th:text="${err}"></p>
Upvotes: 1