Take the 2-minute tour ×

I am doing an user crud in spring-mvc.

My model has the following properties:

private Long id;
private String password;
private String username;
private Collection<Authority> myAuthorities;
private boolean isAccountNonExpired;
private boolean isAccountNonLocked;
private boolean isCredentialsNonExpired;
private boolean isEnabled;

I solved how to show the Authority class in this question.

Now I am willing my form to be able to have a second password field to confirm that the user typed the password correctly.

I don't want to add a confirmPassword property to the model, so my question is how to fix this the best way possible.


Everything is working with axtavt's answer but I am missing a way to validate. I have the following method in my controller, but even though I place a @Validate ApplicationUserFormValidator isn't called.

    @RequestMapping(method = RequestMethod.POST)
 public ModelAndView create(Model model,
   @Valid @ModelAttribute ApplicationUserForm applicationUserFrom,
   BindingResult result) {
  ModelAndView modelAndView = new ModelAndView();

  if (result.hasErrors()) {
  } else {

  return modelAndView;
share|improve this question

3 Answers 3

up vote 2 down vote accepted

You may create an enclosing object to keep a confirmation:

public class ApplicationUserForm {
    private ApplicationUser user;
    private String confirmPassword;



Password: <form:password path = "user.password" />
Confirm password: <form:password path = "confirmPassword" />

Validator also works fine:

public class ApplicationUserFormValidator implements Validator {
    public void validate(Object target, Errors errors) {
        ApplicationUserForm f = (ApplicationUserForm) target;

        if (!f.getConfirmPassword().equals(f.getUser().getPassword())) ...

        new ApplicationUserValidator().validate(f.getUser(), errors);

EDIT: If you use @Valid annotation, you need to register a validator using @InitBinder or in the config as described in the docs. You may also use a fully declarative JSR-303-style validation, but i'm not sure how will it play with business constraints such as user.password == confirmPassword.

public void initBinder(WebDataBinder b) {
    b.setValidator(new ApplicationUserFormValidator());
share|improve this answer
The first part went great, but I just edit my question adding an issue with validation. Can you help me out with this? –  Macarse Aug 17 '10 at 18:37
@Macarse: Edited. –  axtavt Aug 17 '10 at 19:00
Cool, I found that in the docs while looking for the solution, but if I do that I lose the JSR-303 annotations in my model. For instance, my model has: @NotNull @Size(min = 6, max = 12) private String username; –  Macarse Aug 17 '10 at 19:09
@Macarse: Then you need a JSR-303's ConstraintValidator instead of Spring's Validator: static.springsource.org/spring/docs/3.0.x/… –  axtavt Aug 17 '10 at 19:32

add confirmPassword in your applicationUserFrom class
and add check function in setter
it's my best way

private String password;
@NotNull(message="not match")
private String confirmPassword;

public void setPassword(String password) {
    this.password = password;

public void setConfirmPassword(String confirmPassword) {
    this.confirmPassword = confirmPassword;

private void checkPassword() {
    if(this.password == null || this.confirmPassword == null){
    }else if(!this.password.equals(confirmPassword)){
        this.confirmPassword = null;
share|improve this answer

The way that I dealt with this was to put password confirmation and "are you sure you want to do this?" confirmation into the the access policy object that implemented fine-grained access control rules in the controller.

Rather than implementing user management all from scratch, you could use an off-the-shelf solution; e.g. Emmet. (Disclaimer - I'm the author.)

share|improve this answer
I guess I didn't explain myself correctly. I need my form to have a second field for the password. Something like: Password: Confirm password: And then use a validator to check that passwords are equal. –  Macarse Aug 17 '10 at 15:22

Your Answer


