Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8297527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:26:55+00:00 2026-06-08T15:26:55+00:00

I had it working yesterday and then I did something and now I have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:26:58+00:00Added an answer on June 8, 2026 at 3:26 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that I had working in a test but now in
Last night I tried to put together something that I have had working since
I had been working on server side(c#) for a couple of years. But now
I had this SSIS package working yesterday and now I'm getting this error with
Until yesterday Sass was working just fine, now, Sass doesn't compile my SCSS file
I had this working fine yesterday, made some changes and I have no idea
I had started the Rails app yesterday and got everything working. Then today I
I'm working on some big multi threaded project, now yesterday I had a deadlock
Yesterday I had asked a question about pushing Heroku.. it wasn't working, and then
Up until yesterday I had a perfectly working app on my iPhone. I made

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.