Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation messages not picked up from message properties file in Spring
    text
    copied!<p>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.</p> <p>I have a Spring MVC app containing a <code>&lt;form:form&gt;</code> that I want to display custom error messages (<code>&lt;form:errors&gt;</code>) from a .properties file when the user types in wrong information. What 'wrong' is is defined in JSR-303 annotations.</p> <p>Excerpt from the form: </p> <pre><code>&lt;form:form method="post" action="adduserprofile" modelAttribute="bindableUserProfile"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;form:label path="firstName"&gt;Voornaam&lt;/form:label&gt;&lt;/td&gt; &lt;td&gt; &lt;form:input path="firstName"/&gt; &lt;form:errors path="firstName" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;form:label path="lastName"&gt;Achternaam&lt;/form:label&gt;&lt;/td&gt; &lt;td&gt; &lt;form:input path="lastName"/&gt; &lt;form:errors path="lastName" /&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>Excerpt from the BindableUserProfile:</p> <pre><code> @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; } </code></pre> <p>Excerpt from the controller:</p> <pre><code> @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"; } </code></pre> <p>Excerpt from application-context.xml</p> <pre><code> &lt;bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"&gt; &lt;property name="basename" value="messages/messages"/&gt; &lt;/bean&gt; &lt;bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"&gt; &lt;property name="validationMessageSource"&gt; &lt;ref bean="messageSource"/&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>In resources/messages I have two files, messages_en.properties and messages_nl.properties. Both have the same, simple content:</p> <pre><code>errors.requiredfield=This field is required!!! </code></pre> <ul> <li>When I submit the form with an empty first name I can see in the controller method 'addUserProfile()' that errors are indeed found. </li> <li>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.</li> <li>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.</li> <li>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.</li> <li>The empty input is not caught by the NotNull annotation. Spring sees empty input as an empty string and not as null.</li> </ul> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload