Search code examples
springbundlemessagebean-validation

Validation messages not picked up from message properties file in Spring


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!!!
  • When I submit the form with an empty first name I can see in the controller method 'addUserProfile()' that errors are indeed found.
  • When I submit the form with an empty first name the message identifier is shown next to the field, that is, the literal text "errors.requiredfield" or "{errors.requiredfield}" in case of the last name.
  • When I change the message attribute value to "Foo" than "Foo" is shown as a the error message. So the error mechanism itself seems to work fine.
  • The messageSource bean from the application-context.xml must be correct, because it says it can't find the properties files when I change the basename.
  • The empty input is not caught by the NotNull annotation. Spring sees empty input as an empty string and not as null.

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.


Solution

  • 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.