Bean Validation - Change Password

A password class is required to enable bean validation for passwords.

Password.java
package net.java_school.user;

import javax.validation.constraints.Size;

public class Password {

  @Size(min=4, message="The password must be at least 4 characters long.")
  private String currentPasswd;
    
  @Size(min=4, message="The password must be at least 4 characters long.")
  private String newPasswd;

  public String getCurrentPasswd() {
    return currentPasswd;
  }
  public void setCurrentPasswd(String currentPasswd) {
    this.currentPasswd = currentPasswd;
  }
  public String getNewPasswd() {
    return newPasswd;
  }
  public void setNewPasswd(String newPasswd) {
    this.newPasswd = newPasswd;
  }
}
UsersController.java
@RequestMapping(value="/changePasswd", method=RequestMethod.GET)
public String changePasswd(Principal principal, Model model) {
  User user = userService.getUser(principal.getName());
  model.addAttribute("user", user);
  model.addAttribute("password", new Password());
    
  return "users/changePasswd";
}

@RequestMapping(value="/changePasswd", method=RequestMethod.POST)
public String changePasswd(@Valid Password password,
    BindingResult bindingResult,
    Model model,
    Principal principal) {

  if (bindingResult.hasErrors()) {
    User user = userService.getUser(principal.getName());
    model.addAttribute("user", user);
    return "users/changePasswd";
  }
    
  int check = userService.changePasswd(password.getCurrentPasswd(),
        password.getNewPasswd(), principal.getName());
    
  if (check < 1) {
    throw new RuntimeException(WebContants.CHANGE_PASSWORD_FAIL);
  }

  return "redirect:/users/changePasswd_confirm";
}
changePasswd.jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>

<sf:form id="changePasswordForm" action="changePasswd" method="post"
    modelAttribute="password" onsubmit="return check();">
<table>
<tr>
  <td>Current Password</td>
  <td>
    <sf:password path="currentPasswd" /><br />
    <sf:errors path="currentPasswd" cssClass="error" />
  </td>
</tr>
<tr>
  <td>Change password</td>
  <td>
    <sf:password path="newPasswd" /><br />
    <sf:errors path="newPasswd" cssClass="error" />
  </td>
</tr>
<tr>
  <td>Change password Confirm</td>
  <td>
    <input type="password" name="confirm" />
  </td>
</tr>
<tr>
  <td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</sf:form>

On the Change Password screen, press the Submit button without typing anything.

Change password test 1

Change password test 2

References