I am stuck in a big problem with encoding in my website! I use spring 3, tomcat 6, and mysql db. I want to support German and Czech along with English in my website, I creat
First, if your project is using Maven, make sure that the Maven Resources Plugin has UTF-8 set as its character encoding scheme, otherwise the message properties files could be written to your target with an incorrect encoding.
Second, you're using ResourceBundleMessageSource, which uses the standard java.util.ResourceBundle and java.util.Properties, which only support ISO-8859-1 encoding. You can instead use ReloadableResourceBundleMessageSource like:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
which I discovered from this Cake Solutions blog post.
If this still does not work and you're using Tomcat as your application server try to set the following option on every <Connector>
element in the server.xml
:
<Connector URIEncoding="UTF-8" ...>
...
</Connector>
This did the trick for me. There might be similar options for other application servers, so you might want to check the server documentation.
1: By default the messages.properties (Czech) should load, instead the messages_en.properties loads by default.
I don't do Spring, so here's a shoot in the dark: maybe because English is ordered first and/or your platform/browser uses English as default locale? Rearrange the configuration and check accept-language
in the HTTP request header to exclude one and other.
2: In the web form, when I enter Czech data, then click submit, in the Controller I print out the data in the console before to save it to db, what's being printed is not correct having strange chars, and this is the exact data that saves to db.
Is the console configured to use UTF-8 to display data? It would use the platform default encoding otherwise. In for example Eclipse you can configure it by Window > Preferences > General > Workspace > Text File Encoding.
Is the data access layer configured properly to handle the data as UTF-8? MySQL JDBC driver is known to be a bit non-smart in this. It won't use the DB-specified encoding, but the client platform default encoding. To the point, you'd like to use the useUnicode=true
and characterEncoding=UTF-8
properties in the JDBC connection string. E.g.
jdbc:mysql://localhost/some_db?useUnicode=yes&characterEncoding=UTF-8