[Spring MVC - Thymeleaf] - 表单验证和错误消息

时间:2014-12-29 13:32:09

标签: forms validation spring-mvc thymeleaf

我正在尝试创建一个表单并通过命令对象上的@Valid验证其数据。 验证表现良好,但是错误正在回到网络上。

这就是我所拥有的:

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>

控制器

@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; 
    } 
} 

当单击“提交”时,调用“register”方法,result.hasErrors()为true,因此应显示相同的页面,但会发生此错误。

堆栈

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) 

如果我以这种方式将“userForm”添加到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()) { 
        model.addAttribute("userForm", userForm); //THIS LINE IS ADDED 
        return "register"; 
    } else { 
        return "redirect:/" + HomeController.PAGE_NAME; 
    } 
} 

错误消失,但是...... HTML ${#fields.hasErrors('password')}中的表达式结果为false,因此我无法向用户显示错误消息。

知道为什么会发生这种行为? 提前谢谢!

PS:我正在使用Spring MVC 4.1.2和Thymeleaf 2.1.4

1 个答案:

答案 0 :(得分:7)

public String register(final ModelMap model,
        @Valid final UsuarioForm userForm,
        final BindingResult result)

应该是:

public String register(final ModelMap model,
        @ModelAttribute("userForm") @Valid final UsuarioForm userForm,
        final BindingResult result)

注意@ModelAttribute注释。