开发者

Validation errors in freemarker

开发者 https://www.devze.com 2023-03-23 10:19 出处:网络
I try to write password validator with spring and freemarker. BindingResult see errors, but they not showing - spring.status.errorMessages?size returns 0. Validator get correct passwords, because I ch

I try to write password validator with spring and freemarker. BindingResult see errors, but they not showing - spring.status.errorMessages?size returns 0. Validator get correct passwords, because I checked. PasswordForm is a java class with passwords. I put below fragments.

Input in form:

<tr>
    <td><@m.formPassword path='passwordForm.passwordOld' label='user.passwordOld' classes='' attr=''/></td>
    <td><@m.fieldErr /></td>
</tr>

where m:

 "macros.ftl" as m

Used macros from 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>

Controller:

@RequestMapping(method = RequestMethod.POST)
public String onSubmit( 
        @ModelAttribute("passwor开发者_开发技巧dForm") PasswordForm passwordForm,
        BindingResult result,
        ModelMap model,
        SessionStatus sessionStatus,
        HttpSession session) {


    passwordValidator.validate(passwordForm, result);


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


    return  redirect(View.MAIN);
}

Validator:

@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");
    }

}

Anyone can help? There's no errors appears in console.


Your code:

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

It looks like you redirect with an new request. You must not REDIRECT, you have to return the view directly without redirect.


Have a look at my answer to a similar problem. That shows when to use a redirect and when to return the view directly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号