Spring MessageSource not being used during validation

Posted by Jeremy on Stack Overflow See other posts from Stack Overflow or by Jeremy
Published on 2012-06-20T21:15:11Z Indexed on 2012/06/21 3:17 UTC
Read the original article Hit count: 216

Filed under:
|
|
|
|

I can't get my messages in messages.properties to be used during Spring validation of my form backing objects.

app-config.xml:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basename" value="messages" />
</bean>

WEB-INF/classes/messages.properties:

NotEmpty=This field should not be empty.

Form Backing Object:

...
  @NotEmpty
  @Size(min=6, max=25)
  private String password;
...

When I loop through all errors in the BindingResult and output the ObjectError's toString I get this:

Field error in object 'settingsForm' on field 'password': rejected value []; codes [NotEmpty.settingsForm.password,NotEmpty.password,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [settingsForm.password,password]; arguments []; default message [password]]; default message [may not be empty]

As you can see the default message is "may not be empty" instead of my message "This field should not be empty".

I do get my correct message if I inject the messageSource into a controller and output this: messageSource.getMessage("NotEmpty", new Object [] {"password"}, "default empty message", null);

So why isn't the validation using my messages.properties? I'm running Spring 3.1.1. Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about spring