views:

313

answers:

3

Hi All,

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 created all the JSPs as UTF-8 files, and in each jsp I include the following:

<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

I created messages.properties (the default which is Czech), messages_de.properties, and messages_en.properties. And all of them are saved as UTF-8 files.

I added the following to web.xml:

<filter>
    <filter-name>encodingFilter</filter-name>
    <filterclass>
          org.springframework.web.filter.CharacterEncodingFilter</filterclass>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
 </filter>

 <locale-encoding-mapping-list>
    <locale-encoding-mapping>
        <locale>en</locale>
        <encoding>UTF-8</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
        <locale>cz</locale>
        <encoding>UTF-8</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
        <locale>de</locale>
        <encoding>UTF-8</encoding>
    </locale-encoding-mapping>
</locale-encoding-mapping-list>

 <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>  

And add the following to my applicationContext.xml:

<bean id="messageSource"    
    class="org.springframework.context.support.ResourceBundleMessageSource"
    p:basenames="messages"/>

<!-- Declare the Interceptor -->
<mvc:interceptors>    
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
          p:paramName="locale" />
</mvc:interceptors>

<!-- Declare the Resolver -->
<bean id="localeResolver"  
       class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

I set the useBodyEncodingForURI attribute to true in the element of server.xml under: %CATALINA_HOME%/conf, also another time tried to add URIEncoding="UTF-8" instead.

I created all the tables and fields with charset [utf8] and collection [utf8_general_ci]

The encoding in my browser is UTF-8 (BTW, I have IE8 and Firefox 3.6.3)

When I open the MYSQL Query browser and insert manually Czech or German data, it's being inserted correctly, and displayed correctly in my app as well.

So, here's the list of problems I have:

  1. By default the messages.properties (Czech) should load, instead the messages_en.properties loads by default.

  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.

I don't know where's the mistake! Why can't I get it working although I did what people did and worked for them! don't know..

Please help me, I am stuck in this crappy problem since days, and it drives me crazy!

Thank you in advance.

+1  A: 
BalusC
@BalusC Thank you for your suggestions, regarding the first point, I used the CoockieLocaleResolver form spring which will save the user preferences for later times.#2. You were right, the problem with the console was solved by setting the Text File Encoding to URF-8 with eclipse, but still the data stored in the database corrupted! #3 problem to the list of problems, is that the data retrieved from messages_cz.properties does not display correctly in the web pages, it's always corrupted! Although the page encoding is UTF-8!
+1  A: 

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 (now missing) Cake Solutions blog post.

rcampbell
This was the Silver Bullet :-)
A: 

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.

Koraktor