Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring 3.0 MVC seems to be ignoring messages.properties

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!

like image 641
new Thrall Avatar asked Oct 11 '22 15:10

new Thrall


1 Answers

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! :)

like image 135
new Thrall Avatar answered Oct 14 '22 02:10

new Thrall