2

I am getting the following error message when I try to submit a registration form to check for matching password entries in a Spring MVC app using jsp:

org.springframework.context.NoSuchMessageException: 
No message found under code 'Passwords must match.' for locale 'en_US'.

The registration form has a password field and a passwordtest field to make sure that the user did not make a typo. In other words, the user is asked to enter their requested new password twice so that the server can confirm that both attempts match before the new user's password is saved in the database.

How can I change the code below so that Spring performs server side validation to confirm that the user's two password attempts are identical, and to send a useful alert message to the view if they are not identical?

The jsp code for the two fields is:

    <tr>
        <td>
                <spring:bind path="password">
                    <table>
                        <tr>
                            <td>${pwdtest} 
                                <label class="col-sm-2 control-label">Password</label>
                                <form:errors path="password" class="form-error" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <form:input path="password" type="password" id="password" name="password" />
                            </td>
                        </tr>
                    </table>
                </spring:bind>
        </td>
        <td>
                <spring:bind path="passwordtest">
                    <table>
                        <tr>
                            <td>${pwdtest} 
                                <label class="col-sm-2 control-label">Repeat Password</label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <form:input path="passwordtest" type="password" id="passwordtest" name="passwordtest" />
                            </td>
                        </tr>
                    </table>
                </spring:bind>

        </td>
    </tr>

The problem seems to be in the newUserFormValidator.java class, which has the following code:

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import my.app.model.UserCandidate;

@Component
public class NewUserFormValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return UserCandidate.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {

        UerCandidate cand = (UserCandidate) target;

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "field.required", "Email Address is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "field.required", "Password is a required field.");

        if(!cand.getPassword().equals(cand.getPasswordtest())){
            errors.rejectValue("password", "Passwords must match.");
        }
    }
}  

The line of code in the jsp that the stack trace points to is:

<form:errors path="password" class="form-error" />

However, please note that this line of code does successfully print an error when the user leaves the password field empty. So I think the problem is that something else needs to be added.

4

1 回答 1

1

Your error is a global error, not a field error. How would you know which field is wrong if they are filled in but don't match? :)

Use errors.reject("", "Passwords must match");

In your JSP, add an area for global errors:

<spring:hasBindErrors name="nameOfYourModelAttribute">
    <c:if test="${errors.globalErrorCount > 0}">
        <div class="alert alert-danger"><form:errors/></div>
    </c:if>
</spring:hasBindErrors>
于 2015-09-07T21:29:17.157 回答