5

Spring 3.0 MVC

First of all, I haven't found any documentation regarding messages.properties @ springsource Everything I've found about overriding error messages has been on various forums. If anyone has a reference to where messages.properties is documented that'd be fantastic. Maybe messages.properties comes not from spring but a java spec?

I have tried following the advice on JSR-303 Type Checking Before Binding My goal is to replace some type mismatch error messages with my own user friendly error messages

My situation is as follows:

Model

public class Test {

    private int numberbomb;

    public int getNumberbomb() {
        return numberbomb;
    }

    public void setNumberbomb(int numberbomb) {
        this.numberbomb = numberbomb;
    }
}

myservlet.xml

<mvc:annotation-driven/>

jsp

<form:form id="test" method="post" modelAttribute="test">

<form:errors path="*"/>

<form:label path="numberbomb">numberbomb</form:label>
<form:input path="numberbomb"/>

</form:form>

classes\messages.properties

typeMismatch=bad value you bad bad person
test.numberbomb=you are driving me crazy with these bad inputs

Output from the form

Failed to convert property value of type java.lang.String to required type int for property numberbomb; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "three" from type java.lang.String to type int; nested exception is java.lang.NumberFormatException: For input string: "three"

BindingResult.toString() within my controller

Field error in object 'test' on field 'numberbomb': rejected value [three]; codes [typeMismatch.test.numberbomb,typeMismatch.numberbomb,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [test.numberbomb,numberbomb]; arguments []; default message [numberbomb]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'numberbomb'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "three" from type 'java.lang.String' to type 'int'; nested exception is java.lang.NumberFormatException: For input string: "three"]

Is displaying the error messages with <form:errors> the wrong way display custom error messages? Do I need to add something to spring config files to tell it to look at messages.properties? Spring seems to be ignoring my messages.properties file (which is located in the WEB-INF\classes folder)

Thanks for any ideas!

Community
  • 1
  • 1
new Thrall
  • 970
  • 1
  • 12
  • 20

1 Answers1

4

An associate of mine pointed me in the right direction. I changed the messageSource bean in myservlet.xml

from

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

to

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

For whatever reason this solved the problem. Thanks associate! :)

new Thrall
  • 970
  • 1
  • 12
  • 20
  • 2
    That reason can be found in the documentation! - http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/ReloadableResourceBundleMessageSource.html - "..supports reloading of properties files through the "cacheSeconds" setting, and also through programmatically clearing the properties cache. Since application servers typically cache all files loaded from the classpath, it is necessary to store resources somewhere else (for example, in the "WEB-INF" directory of a web app). Otherwise changes of files in the classpath will not be reflected in the application." – Ross Jun 01 '15 at 00:57