2014-12-29 138 views
2

我試圖在命令對象上通過@Valid創建表單並驗證其數據。 驗證執行得很好,但錯誤正在返回網絡。[Spring MVC - Thymeleaf] - 表單驗證和錯誤消息

這是我有:

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

「提交」,當點擊 「註冊」 方法被調用,結果。 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) 

如果我 「窗體」,在控制這種方式添加到模型:

控制器修改

@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')}結果中的表達是錯誤的,所以我不能顯示錯誤消息給用戶。

任何想法爲什麼會發生這種行爲? 在此先感謝!

PS:我使用Spring MVC的4.1.2與2.1.4 Thymeleaf

回答

5

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註解。

+0

完美的工作。非常感謝和新年快樂。 關心! –