62767

Spring MVC custom errors and internationalization

Question:

In my web application, I handle errors with annotations. Everything works fine and I can use custom messages via the "message" parameter.

@Digits(fraction = 0, integer = 3, message="my custom error message...") private String price;

Now I'm trying to internationalize this message with a .properties files, but I certainly miss something and I can't get it to work.

My spring config :

<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <beans:property name="basenames" value="classpath:i18n/messages, classpath:i18n/errors" /> <beans:property name="defaultEncoding" value="UTF-8" /> </beans:bean> <beans:bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <beans:property name="validationMessageSource"> <beans:ref bean="messageSource" /> </beans:property> </beans:bean> <beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver"> <beans:property name="defaultLocale" value="fr" /> </beans:bean>

My new bean :

@Digits(fraction = 0, integer = 3) private String price;

My "errors_fr.properties" file. I've already tried everything :

Digits.myBean.myNestedBean.price = my custom error message... Digits.myNestedBean.price = my custom error message... javax.validation.constraints.Digits.myNestedBean.price = my custom error message...

I always get the same generic message from spring, it's like as spring doesn't detect my .properties file. By the way, the message keys above can be found in the BindingResult object when debugging.

What am I missing here ?

Notice that I already have internationalized messages in my jsp's (in the "messages_fr.properties" file) and they work fine.

Answer1:

I had a similar problem in my application, and I hope that this can help you.

As discussed in this thread, <a href="http://forum.springsource.org/showthread.php?73240-Roo-JSR-303-Validations-and-Localization" rel="nofollow">http://forum.springsource.org/showthread.php?73240-Roo-JSR-303-Validations-and-Localization</a>, you need to:

<ol><li>define the error messages referred by the annotation in file ValidationMessages.properties</li> <li>in your annotation, refer to the error message key enclosed in curly brackets:</li> </ol>

@Digits(fraction = 0, integer = 3, message="{message.key}")

Hope this helps.

Recommend

  • how to change this recursion to tail recursion?
  • Possible to stop MDB in Weblogic 8.x?
  • selective filter of join of three tables
  • SQL QUERY showing Between Dates as specific dates + Data belonging to each date!
  • How can I replace my .xib file with pure Swift 3?
  • SessionInformation expiration doesn't lead to user log out from system
  • Spring roo - how to install spring surf
  • Insert null in a database
  • Spring @Transactional - javax.persistence.TransactionRequiredException
  • What is the likely cause of a net::ERR_CONNECTION_ABORTED when uploading a file to Spring
  • Wrapping a c#/WPF GUI around c++/cli around native c++
  • Microsoft Excel Pivot miscalculation in Sum for positive and negative numbers
  • Unable to click on the next page button containing “>” sign
  • Is there a way to set up a fallback for the formAction attribute in HTML5?
  • Can I use AllJoyn Framework for Wifi Direct in iOS?
  • Spring: No transaction manager has been configured
  • Enabling DTD support in Sql Server
  • JPA flush vs commit
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • What and where is mdimport
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • Hardware Accelerated Image Scaling in windows using C++
  • Read a local file using javascript
  • ImageMagick, replace semi-transparent white with opaque white
  • Cannot connect to cassandra from Spark
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Cross-Platform Protobuf Serialization
  • Javascript Callbacks with Object constructor
  • Can I make an Android app that runs a web view in Chrome 39?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • How to include full .NET prerequisite for Wix Burn installer
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?