<img height="1" width="1" src="https://www.facebook.com/tr?id=1076094119157733&amp;ev=PageView &amp;noscript=1">

UTF-8 encoding and Spring message sources

Posted by Aleksa Vukotic on Thu, Apr 2, 2009

Take a look at the update from October 2011's perspective here.

I was working on a pretty much straight-forward web application. As usual, i used Spring's org.springframework.context.MessageSourcesupport, to be more precise, i used standard implementation - ResourceBundleMessageSource. Simple configuration, as following:

&lt;bean id="messageSource"
          &lt;property name="basename" value="messages"/&gt;

My messages properties file was standard as well, country names, so i had something like this:


As you can guess, the problems developed with specific characters in country names(ë, Í...). When i run the application, i got '???' characters instead of specific UTF-8 characters for countries (so 'België' become 'Belgi???').

After some research, and digging in Spring source code, i located the problem, and the solution!

The problem was that ResourceBundleMessageSource uses the standard java.util.ResourceBundle and
java.util.Properties, which only support ISO-8859-1 encoding - so no UTF-8 encoding, no support for any special French, Spanish or Asian characters.

The solutions is very simple, and it comes with Spring as well. Use ReloadableResourceBundleMessageSource instead! Here is the correct configuration:

&lt;bean id="messageSource";
         &lt;property name="basename" value="classpath:messages"/&gt;
         &lt;property name="defaultEncoding" value="UTF-8"/&gt;

As you can see, we are now able ot set default encoding explicitelly, so the countries names in my example worked like charm. The only other change was that the basename is now specified as Resource, so i was able to use convenient classpath: identifier to specify my messages.properties wherever i need it in the source code.

Need help on your next project?


Topics: spring mvc, spring

Recent Posts

Posts by Topic

see all

Subscribe to Email Updates