Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何在freemarker模板中显示验证错误_Spring_Validation_Spring Mvc_Freemarker - Fatal编程技术网

Spring 如何在freemarker模板中显示验证错误

Spring 如何在freemarker模板中显示验证错误,spring,validation,spring-mvc,freemarker,Spring,Validation,Spring Mvc,Freemarker,我在freemarker模板中显示验证错误的所有方法都失败。我使用SpringMVC版本3 我的表格是这样的 <@layout.basic> <@spring.bind "user" /> <#if spring.status.error> <div class="errors"> There were problems with the data you entered: <ul>

我在freemarker模板中显示验证错误的所有方法都失败。我使用SpringMVC版本3

我的表格是这样的

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

验证就像一个符咒,但当发生错误时,视图不会改变,也不会显示错误。我一遍又一遍地阅读文档,但找不到解决问题的方法。我的错误是什么?

我不熟悉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
) { 
    ...
}

嘿,谢谢你的快速回答,但我今天不能测试这段代码。下一次我将在@work上试用。我测试了你的提示,效果很好!!再次感谢:)如果可以,我会投票支持你;)
<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";
}
@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
) { 
    ...
}