Spring MVC - Form Validation using Java Bean Validation API and External Message Source

[Last Updated: Nov 27, 2017]

Following example shows how to configure and map custom validation error codes to external messages while implementing Java Bean Validation in a Spring MVC application. We are going to reuse our last example. We just need to add our custom message codes with Java Bean Validation annotations and include an external message source.


A Java Bean

public class User {
  private Long id;

  @Size(min = 5, max = 20, message = "{}")
  private String name;

  @Size(min = 6, max = 15, message = "{user.password.size}")
  @Pattern(regexp = "\\S+", message = "{user.password.pattern}")
  private String password;

  @NotEmpty(message = "{}")
  @Email(message = "{}")
  private String emailAddress;

Message Source

src/main/resources/ Name must be of more than 5 and less than 20 characters.
user.password.size=User Password length must of between 6 and 15.
user.password.pattern=Password must not have spaces. Email cannot be empty. is not valid.

Spring Controller

public class UserRegistrationController {

  private UserService userService;

  @RequestMapping(method = RequestMethod.GET)
  public String handleGetRequest (Model model) {
      model.addAttribute("user", new User());
      return "user-registration";

  @RequestMapping(method = RequestMethod.POST)
  public String handlePostRequest (@Valid @ModelAttribute("user") User user,
                                   BindingResult bindingResult, Model model) {
      if (bindingResult.hasErrors()) {
          return "user-registration";

      return "registration-done";

JSP Form


<%@taglib uri="" prefix="frm"%>
span.error {
color: red;
display: inline-block;

<h3> Registration Form <h3>
 <frm:form action="register" method="post" commandName="user">
                  Name <frm:input path="name" />
                       <frm:errors path="name" cssClass="error" />

         Email address <frm:input path="emailAddress" />
                       <frm:errors path="emailAddress" cssClass="error" />

              Password <frm:password path="password" />
                       <frm:errors path="password" cssClass="error" />

                                  <input type="submit" value="Submit" />

Java Config

public class MyWebConfig {

  public MessageSource messageSource() {
      ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
      return messageSource;

  public UserRegistrationController userRegistrationController() {
      return new UserRegistrationController();

  public UserService userService() {
      return new InMemoryUserService();

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war


Submitting invalid values:

Example Project

Dependencies and Technologies Used:

  • spring-webmvc 4.2.4.RELEASE: Spring Web MVC.
  • spring-test 4.2.4.RELEASE: Spring TestContext Framework.
  • javax.servlet-api 3.0.1 Java Servlet API
  • hibernate-validator 5.2.4.Final: Hibernate's Bean Validation (JSR-303) reference implementation.
  • junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
  • JDK 1.8
  • Maven 3.3.9

Spring Form Validation Message Code Select All Download
  • spring-form-message-code
    • src
      • main
        • java
          • com
            • logicbig
              • example
          • resources
          • webapp
            • WEB-INF
              • views
        • test
          • java
            • com
              • logicbig
                • example

    See Also