vendredi 6 mars 2015

Spring validator annotation and error redirection

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 ?


thanks for advance


Aucun commentaire:

Enregistrer un commentaire