【问题标题】:Validation errors in freemarkerfreemarker 中的验证错误
【发布时间】: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


【解决方案1】:

您的代码:

if(result.hasErrors())
      return redirect(view);

您似乎使用新请求进行了重定向。你不能重定向,你必须直接返回视图而不重定向。


查看my answer 的类似问题。这显示了何时使用重定向以及何时直接返回视图。

【讨论】:

    猜你喜欢
    • 2011-11-25
    • 2011-04-21
    • 1970-01-01
    • 2017-01-15
    • 2011-05-22
    • 2015-11-24
    • 2015-08-12
    • 2014-01-13
    • 2011-07-16
    相关资源
    最近更新 更多