2010-10-06 94 views
4

所有的答案我都會在freemarker模板失敗時顯示我的驗證錯誤。我使用Spring MVC的版本3.howto在freemarker模板中顯示驗證錯誤

我的形式看起來像

<@layout.basic> 
<@spring.bind "user" /> 
<#if spring.status.error> 
    <div class="errors"> 
     There were problems with the data you entered: 
     <ul> 
      <#list spring.status.errorMessages as error> 
       <li>${error}</li> 
      </#list> 
     </ul> 
    </div> 
<#else> 
    <div class="errors"> 
     There are no errors. 
    </div> 
</#if> 
<form action="" method="POST"> 
    <dl> 
     <dt>Login:</dt> 
      <dd><@spring.formInput "user.name" /> 
      <dd><@spring.showErrors "<br>" /> 
     <dt>E-Mail:</dt> 
      <dd><@spring.formInput "user.email" /> 
      <dd><@spring.showErrors "<br>" /> 
     <dt>Password:</dt> 
      <dd><@spring.formPasswordInput "user.passwort" /> 
      <dd><@spring.showErrors "<br>" /> 
     <dt>Password verification:</dt> 
      <dd><input type="password" name="passVerification"/> 
      <dd><@spring.showErrors "<br>" /> 
     <dt>Should the User have administrator rights?</dt> 
      <dd><input type="checkbox" name="isAdmin"/> 
      <dd><@spring.showErrors "<br>" /> 
     <br> 
      <dd><input type="submit" value="Create" /> 
    </dl> 
</form> 

我的基本佈局看起來像

<#macro basic> 
<#-- needed for query spring security status --> 
<#assign security=JspTaglibs["http://www.springframework.org/security/tags"] /> 
<!DOCTYPE HTML> 
<html> 
    <head> 
     <title>Testform</title> 
    </head> 
    <body> 
     <div id=header> 
      <@security.authorize ifAnyGranted="ROLE_ADMIN"> 
       <a href='<@spring.url "/user/add" />'>Add user | </a> 
       <a href='<@spring.url "/user/manage" />'>Manage users | </a> 
      </@security.authorize>  
      <@security.authorize ifAnyGranted="ROLE_USER"> 
       <a href='<@spring.url "/job/add" />'>Add job | </a> 
       <a href='<@spring.url "/job/show" />'>Show jobs | </a> 
      </@security.authorize> 
     </div> 
     <div id=errors> 
     </div> 
     <div id=content> 
      <#nested> 
     </div> 
     <div id=footer> 
      <@security.authorize ifAnyGranted="ROLE_USER"> 
       <a href='<@spring.url "/j_spring_security_logout" />'>Logout</a> 
      </@security.authorize> 
     </div> 
    </body> 
</html> 
</#macro> 

我在servlet配置中定義的spring.ftl

<property name="freemarkerSettings"> 
    <props> 
     <prop key="auto_import">layout.ftl as layout, spring.ftl as spring</prop> 
    </props> 
</property> 

我的控制器看起來像這樣

@RequestMapping(value = "/add", method = RequestMethod.POST) 
public String addUser(
     @RequestParam(value="isAdmin",defaultValue="false") Boolean isAdmin, 
     @RequestParam(value="passVerification") String passVerification, 
     @ModelAttribute("user") C_UserDAO newUser 
) { 
    final BindException errors = new BindException(newUser, "user"); 
    m_userValidator.validate(newUser, errors); 
    ... 
    if(!newUser.getPassword().equals(passVerification) && !newUser.getPassword().equals("")) { 
     errors.rejectValue("password", "user.password.missmatch", "The passwords aren't equal, try again"); 
    } 
    if(errors.hasErrors()) { 
     return "addUserForm"; 
    } 
    ... 
    return "redirect:thanks.html"; 
} 

驗證用得好好的,但是當一個錯誤occures,認爲沒有變化,沒有顯示任何錯誤。我一遍又一遍地閱讀文檔,但是我找不到如何解決這個問題。我的錯誤是什麼?

回答

13

我對FreeMarker並不熟悉,但我看到您的BindingResult與該模型沒有關聯。您需要將其添加到相應模型屬性後的方法簽名中:

@RequestMapping(value = "/add", method = RequestMethod.POST) 
public String addUser( 
     @RequestParam(value="isAdmin",defaultValue="false") Boolean isAdmin, 
     @RequestParam(value="passVerification") String passVerification, 
     @ModelAttribute("user") C_UserDAO newUser, 
     BindingResult errors 
) { 
    ... 
} 
+0

嘿,感謝您的快速回答,但我今天無法測試此代碼。我會在下次嘗試時再嘗試。 – lofthouses 2010-10-06 15:38:54

+0

我測試了你的提示,它的工作原理!再次感謝:)如果我能,我會投票給你;) – lofthouses 2010-10-12 08:34:19