我所做的所有approches都在freemarker模板中显示我的验证错误失败。我使用spring mvc version 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 config中定义了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";
}
验证就像魅力一样,但是当发生错误时,视图不会改变,也不会显示错误。我一遍又一遍地阅读文档,但我找不到如何解决问题。我的错误是什么?
答案 0 :(得分:13)
我不熟悉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
) {
...
}