4

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?

1 Answer 1

13

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
) { 
    ...
}
2
  • Hey, thank you for your fast answer, but i can't test this code today. I will try it the next time i'll be @work.
    – lofthouses
    Oct 6, 2010 at 15:38
  • I tested your hint and it works!! Thanks again :) If i could, i would vote you up ;)
    – lofthouses
    Oct 12, 2010 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.