Howto display validation errors in freemarker template

all the matches that I make to display validation errors in the freemarker template fail. I am using spring mvc version 3.

My form looks like this

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

My main layout looks like this:

 <#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> 

I defined spring.ftl in my servlet configuration

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

And my controller is as follows

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

Validation works like a charm, but when an error occurs, the view does not change and the error is not displayed. I read the documentation again and again, but I cannot find clou how to solve the problem. What is my mistake?

+4
source share
1 answer

I am not familiar with FreeMarker, but I see that your BindingResult not model related. You need to add it to the signature of your method immediately after the corresponding model attribute:

 @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 ) { ... } 
+13
source

All Articles