title: org.springframework.context.NoSuchMessageException: No message found under code 'Size.userForm.username' for locale 'en_US'
body: <p>I am trying to display error messages to the user when he registers on website. I am using spring, spring validation, spring security and mysql database. I am new to spring, and I can't figure this out. </p>
  
  <p>Here is my html file:</p>
  
  <pre><code>&lt;%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %&gt;
  &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  &lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %&gt;
  
  &lt;c:set var="contextPath" value="${pageContext.request.contextPath}"/&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
      &lt;!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --&gt;
      &lt;meta name="description" content=""&gt;
      &lt;meta name="author" content=""&gt;
  
      &lt;title&gt;Create an account&lt;/title&gt;
  
      &lt;link href="${contextPath}/resources/css/bootstrap.min.css" rel="stylesheet"&gt;
      &lt;link href="${contextPath}/resources/css/common.css" rel="stylesheet"&gt;
  
      &lt;!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --&gt;
      &lt;!--[if lt IE 9]&gt;
      &lt;script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"&gt;&lt;/script&gt;
      &lt;script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"&gt;&lt;/script&gt;
      &lt;![endif]--&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;div class="container"&gt;
  
      &lt;form:form method="POST" modelAttribute="userForm" class="form-signin"&gt;
          &lt;h2 class="form-signin-heading"&gt;Create your account&lt;/h2&gt;
          &lt;spring:bind path="username"&gt;
              &lt;div class="form-group ${status.error ? 'has-error' : ''}"&gt;
                  &lt;form:input type="text" path="username" class="form-control" placeholder="Username"
                              autofocus="true"&gt;&lt;/form:input&gt;
                  &lt;form:errors path="username"&gt;&lt;/form:errors&gt;
              &lt;/div&gt;
          &lt;/spring:bind&gt;
  
          &lt;spring:bind path="password"&gt;
              &lt;div class="form-group ${status.error ? 'has-error' : ''}"&gt;
                  &lt;form:input type="password" path="password" class="form-control" placeholder="Password"&gt;&lt;/form:input&gt;
                  &lt;form:errors path="password"&gt;&lt;/form:errors&gt;
              &lt;/div&gt;
          &lt;/spring:bind&gt;
  
          &lt;spring:bind path="passwordConfirm"&gt;
              &lt;div class="form-group ${status.error ? 'has-error' : ''}"&gt;
                  &lt;form:input type="password" path="passwordConfirm" class="form-control"
                              placeholder="Confirm your password"&gt;&lt;/form:input&gt;
                  &lt;form:errors path="passwordConfirm"&gt;&lt;/form:errors&gt;
              &lt;/div&gt;
          &lt;/spring:bind&gt;
  
          &lt;button class="btn btn-lg btn-primary btn-block" type="submit"&gt;Submit&lt;/button&gt;
      &lt;/form:form&gt;
  
  &lt;/div&gt;
  &lt;!-- /container --&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="${contextPath}/resources/js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My dispatcher-servlet looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:p="http://www.springframework.org/schema/p"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd"&gt;
  
      &lt;context:component-scan base-package="com.web.shop"/&gt;
  
      &lt;mvc:annotation-driven/&gt;
  
      &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix" value="/WEB-INF/view/"/&gt;
          &lt;property name="suffix" value=".jsp"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="messageSource"
       class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
       p:basename="WEB-INF/messages" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>My UserValidation file:</p>
  
  <pre><code>@Component
  public class UserValidator implements Validator {
  
      @Autowired
      private UserService userService;
  
      @Override
      public boolean supports(Class&lt;?&gt; aClass) {
          return User.class.equals(aClass);
      }
  
      @Override
      public void validate(Object o, Errors errors) {
          User user = (User) o;
  
          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "NotEmpty");
  
          if (user.getUsername().length() &lt; 6 || user.getUsername().length() &gt; 32) {
              errors.rejectValue("username", "Size.userForm.username");
          }
  
          if (userService.findByUsername(user.getUsername()) != null) {
              errors.rejectValue("username", "Duplicate.userForm.username");
          }
  
          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty");
          if (user.getPassword().length() &lt; 8 || user.getPassword().length() &gt; 32) {
              errors.rejectValue("password", "Size.userForm.password");
          }
  
          if (!user.getPasswordConfirm().equals(user.getPassword())) {
              errors.rejectValue("passwordConfirm", "Diff.userForm.passwordConfirm");
          }
      }
  }
  </code></pre>
  
  <p>My file with the messages is in WEB-INF in messages folder, it is called validation.properties and look like this:</p>
  
  <pre><code>NotEmpty=This field is required.
  Size.userForm.username=Please use between 6 and 32 characters.
  Duplicate.userForm.username=Someone already has that username.
  Size.userForm.password=Try one with at least 8 characters.
  Diff.userForm.passwordConfirm=These passwords don't match.
  </code></pre>
  
  <p>I don't understand why cant it find the messages. This is the error that I am getting:</p>
  
  <pre><code>There was an unexpected error (type=Internal Server Error, status=500).
  org.springframework.context.NoSuchMessageException: No message found under code 'Size.userForm.username' for locale 'en_US'.
  </code></pre>
  
  <p>Thank you for your help.</p>