I had it working yesterday and then I did something and now I have been trying to fix it for hours and I just can't get it to work anymore.
I have a Spring MVC app containing a <form:form>
that I want to display custom error messages (<form:errors>
) from a .properties file when the user types in wrong information. What 'wrong' is is defined in JSR-303 annotations.
Excerpt from the form:
<form:form method="post" action="adduserprofile" modelAttribute="bindableUserProfile">
<table>
<tr>
<td><form:label path="firstName">Voornaam</form:label></td>
<td>
<form:input path="firstName"/>
<form:errors path="firstName" />
</td>
</tr>
<tr>
<td><form:label path="lastName">Achternaam</form:label></td>
<td>
<form:input path="lastName"/>
<form:errors path="lastName" />
</td>
</tr>
Excerpt from the BindableUserProfile:
@NotNull
@Size(min = 3, max = 40, message="{errors.requiredfield}")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@NotNull
@Size(min = 3, max = 40, message="errors.requiredfield")
public String getLastName() {
return lastName;
}
Excerpt from the controller:
@RequestMapping(value = "/edit/{userProfileId}", method = RequestMethod.GET)
public String createOrUpdate(@PathVariable Long userProfileId, Model model) {
if (model.containsAttribute("bindableUserProfile")) {
model.addAttribute("userProfile", model.asMap().get("bindableUserProfile"));
} else {
UserProfile profile = userProfileService.findById(userProfileId);
if (profile != null) {
model.addAttribute(new BindableUserProfile(profile));
} else {
model.addAttribute(new BindableUserProfile());
}
}
model.addAttribute("includeFile", "forms/userprofileform.jsp");
return "main";
}
@RequestMapping(value = "/adduserprofile", method = RequestMethod.POST)
public String addUserProfile(@Valid BindableUserProfile userProfile, BindingResult result, Model model) {
if (result.hasErrors()) {
return createOrUpdate(null, model);
}
UserProfile profile = userProfile.asUserProfile();
userProfileService.addUserProfile(profile);
return "redirect:/userprofile";
}
Excerpt from application-context.xml
<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages/messages"/>
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="messageSource"/>
</property>
</bean>
In resources/messages I have two files, messages_en.properties and messages_nl.properties. Both have the same, simple content:
errors.requiredfield=This field is required!!!
So, it seems the properties files are found and the validation annotations are properly processed, but Spring doesn't understand it must replace the message keys with messages from the properties files.
Yaaaargh, I think this was never supposed to work in the first place.
I thought it was possible to have the "message" attribute of JSR-303 annotations to be interpreted as a key in order to get an associated error message from a message.properties file, but I thinnk I am wrong.
@Size(min = 3, max = 40, message="errors.requiredfield")
My collegue at work programmed a layer that created this behaviour for us, but it doesn't work by default. It seemed as if I had it working once, because I was using
@Size(min = 3, max = 40, message="{errors.requiredfield}")
The curly braces caused Spring to start a find and replace procedure that uses .properties files as a source. This second option still worked though.