Internationalization- How to store and retrieve Japanese characters in Spring MVC

Hello,

If you want to store and retrieve Japanese characters in Spring framework using MySQL and Hibernate follow the following steps.

Step1: You should have Japanese language support installed on your system
 or change your system locale






Step 2: Change you project text encoding as UTF-8 
            For eclipse: Window->Preference->General->Workspace
            For Netbeans: Right click on project > properties > sources >Encoding.

Step 3: Place Spring framework’s in built character encoding filter as a first filter in filter chain (web.xml) to make sure it runs first during request processing and runs last during response processing

web.xml

    <filter>  
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
         <!-- set forceEncoding to true if you want to override encoding of servlet -->
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value> 
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


Step 4: Set JSP Page encoding to UTF-8

<%@page contentType="text/html" pageEncoding="UTF-8"%>

Step 5: Set Hibernate connection encoding scheme to UTF-8 

<property name="hibernate.connection.characterEncoding">UTF-8</property>

Step 6: Add i18n interceptor 

<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
</bean>

Step 7: For the connection url you have to append characterEncoding=UTF-8

jdbc:mysql://{ipaddress}:{port}/{database_name}?characterEncoding=UTF-8

Step 8: Your database should have COLLATE='utf8_general_ci' and Charset = UTF8 and also your table should have the same.
Use this query if necessary to apply on each table.
ALTER TABLE `{table_name}` COLLATE='utf8_general_ci', CONVERT TO CHARSET utf8; 

Done! Ready to go...


Post a Comment

1 Comments