all approches i do to display my validation errors in a freemarker template fail. i use spring mvc version 3.
My form looks like that
<@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 basic layout looks like that
<#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 the spring.ftl in my servlet config
<property name="freemarkerSettings">
<props>
<prop key="auto_import">layout.ftl as layout, spring.ftl as spring</prop>
</props>
</property>
And my Controller looks like this
@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";
}
The validation works like a charm, but when an error occures, the view doesn't change and no error is shown. I've read the documentation over and over again but i can't find a clou how to solve the problem. What's my error?
I'm not familiar with FreeMarker, but I see your BindingResult
is not associated with the model. You need to add it to the signature of your method right 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
) {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With