【问题标题】:howto display validation errors in freemarker template如何在 freemarker 模板中显示验证错误
【发布时间】:2011-04-21 20:38:45
【问题描述】:

我在 freemarker 模板中显示我的验证错误的所有方法都失败了。我使用 spring mvc 版本 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>

我的基本布局是这样的

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

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

【问题讨论】:

    标签: spring validation spring-mvc freemarker


    【解决方案1】:

    我不熟悉 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时我会试试的。
    • 我测试了你的提示,它有效!再次感谢 :) 如果可以的话,我会投票给你 ;)
    猜你喜欢
    • 2011-11-25
    • 2017-01-15
    • 2017-10-04
    • 2015-11-24
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 2017-09-15
    相关资源
    最近更新 更多