【发布时间】:2011-10-14 03:50:43
【问题描述】:
我尝试用 spring 和 freemarker 编写密码验证器。 BindingResult 看到错误,但它们没有显示 - spring.status.errorMessages?size 返回 0。验证器获得正确的密码,因为我检查过。 PasswordForm 是一个带有密码的 java 类。我把片段放在下面。
表单输入:
<tr>
<td><@m.formPassword path='passwordForm.passwordOld' label='user.passwordOld' classes='' attr=''/></td>
<td><@m.fieldErr /></td>
</tr>
哪里米:
"macros.ftl" as m
使用来自 macros.ftl 的宏:
<#macro fieldErr>
<#if (spring.status.errorMessages?size > 0)>
<label> blaaad</label>
</#if>
<label> size=${spring.status.errorMessages?size}</label>
</#macro>
<#macro formPassword path label='' classes='' attr='' required=false>
<@spring.bind "${path}" />
<#assign error = '' />
<#if (spring.status.errorMessages?size > 0)>
<#assign error = 'err' /></#if>
<#if label??>
<br/>
<label class="inputLabel ${error} <#if required>required </#if>"><@spring.message code="${label}" /></label>
</#if>
<@spring.formPasswordInput path="${path}" attributes='class="inputText ${classes} ${error}" AUTOCOMPLETE="off" ' />
<#if (spring.status.errorMessages?size > 0)>
<span class="err">${spring.status.errorMessage}</span>
</#if>
</#macro>
控制器:
@RequestMapping(method = RequestMethod.POST)
public String onSubmit(
@ModelAttribute("passwordForm") PasswordForm passwordForm,
BindingResult result,
ModelMap model,
SessionStatus sessionStatus,
HttpSession session) {
passwordValidator.validate(passwordForm, result);
if(result.hasErrors())
return redirect(view);
return redirect(View.MAIN);
}
验证器:
@Override
public void validate(Object target, Errors errors) {
PasswordForm passwordForm = (PasswordForm) target;
String passwordOld = passwordForm.getPasswordOld();
String passwordNew = passwordForm.getPasswordNew();
String passwordNewRepeat = passwordForm.getPasswordNewRepeat();
if (StringUtils.isBlank(passwordOld)) {
errors.rejectValue("passwordOld", "password.blank");
}
if (!StringUtils.equals(passwordNew, passwordNewRepeat)) {
errors.rejectValue("passwordNewRepeat", "password.notmatch");
errors.rejectValue("passwordNew", "password.notmatch");
}
}
有人可以帮忙吗?控制台中没有出现错误。
【问题讨论】:
-
已解决。我在 onSubmit 方法中做了错误的重定向。
标签: spring macros freemarker