[Spring MVC - Thymeleaf]- Formular-Validierung und Fehlermeldungen
Ich versuche ein Formular erstellen und validieren Sie Ihre Daten via @Gültig auf das command-Objekt.
Die Validierung funktioniert gut, aber ein Fehler ist Belastungen gehen zurück zum web.
Dies ist, was ich habe:
HTML
<div id="content" layout:fragment="contenido">
<div sec:authorize="isAnonymous()">
<form class="form-horizontal" action="#" th:action="@{register}" th:object="${userForm}" method="post">
<input type="hidden" name="_csrf" th:value="${_csrf.token}"/>
<fieldset>
<label for="alias" th:text="#{form.register.alias}">Alias</label>
<input id="alias" type="text" th:field="*{alias}" placeholder="Su alias" required="required" autofocus="autofocus"/>
<label for="pass" th:text="#{form.register.password}">Contraseña</label>
<input id="pass" type="password" th:field="*{password}" pattern="[\w\d-_]{5,15}" required="required" th:title="#{form.error.password}"/>
<p th:if="${#fields.hasErrors('password')}" th:errors="*{password}">Error en el dato ingresado</p>
<button type="submit" name="save" class="btn btn-primary" th:text="#{control.register}">Registrarme</button>
</fieldset>
</form>
</div>
</div>
Controller
@RequestMapping(value = "/register", params = {"save"}, method = RequestMethod.POST)
public String register (final ModelMap model, @Valid final UsuarioForm userForm, final BindingResult result) {
if (result.hasErrors()) {
return "register";
} else {
return "redirect:/" + HomeController.PAGE_NAME;
}
}
Beim Klick auf "senden", die "register" - Methode aufgerufen wird, das Ergebnis.hasErrors() true, so dass die gleiche Seite angezeigt werden sollen, aber dieser Fehler Auftritt.
Stack
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userForm' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:396)
org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:323)
org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:289)
org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.processAttribute(AbstractSpringFieldAttrProcessor.java:98)
org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87)
org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212)
org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017)
org.thymeleaf.dom.Node.processNode(Node.java:972)
Wenn ich "userForm", um das Modell in der Steuerung auf diese Weise:
Controller Geändert
@RequestMapping(value = "/register", params = {"save"}, method = RequestMethod.POST)
public String register (final ModelMap model, @Valid final UsuarioForm userForm, final BindingResult result) {
if (result.hasErrors()) {
model.addAttribute("userForm", userForm); //THIS LINE IS ADDED
return "register";
} else {
return "redirect:/" + HomeController.PAGE_NAME;
}
}
Der Fehler verschwindet, ABER... der Ausdruck in der HTML - ${#fields.hasErrors('password')}
Ergebnisse falsch, so kann ich nicht zeigen, der Fehlermeldungen an den Benutzer.
Keine Idee warum dieses Verhalten ist passiert?
Vielen Dank im Voraus!
PS: ich bin mit Spring MVC 4.1.2 mit Thymeleaf 2.1.4
InformationsquelleAutor Emiliano Schiano | 2014-12-29
Du musst angemeldet sein, um einen Kommentar abzugeben.
Diese
werden sollte:
Beachten Sie die
@ModelAttribute
annotation.InformationsquelleAutor Bohuslav Burghardt