How to hold japanese characters in SPRING MVC POJO's field

浪尽此生 提交于 2019-12-01 11:51:28

Finally I succeed in implementing internationalization support in my Spring MVC based application....

I have followed below steps to incorporate internationalization support to my web application using Spring MVC, Hibernate , MYSQL or Oracle Database and Jboss or webLogic as a application server.

Let's say we want to add internationalization support for Japanese language.i.e user should be able to enter Japanese characters in web forms and it should be saved in same format as entered by user and also should be displayed in same language on web page.

Follow below steps.

  1. Make sure you have Japanese language support (Region specific installation) installed into operating system. If not, please install it.

  2. If you are using any IDE then configure IDE to support Japanese language by changing text encoding to UTF-8. For example,If you are using my Eclipse then change text file encoding to UTF-8. You can change by this path (Window->Preference->General->Workspace)

  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-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

4 Set JSP Page encoding to UTF-8 by adding below mentioned code at top of JSP.

<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

5 Set Hibernate connection encoding scheme to UTF-8 by adding following property to hibernate-cfg.xml

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

Note : If using JBoss application server, please make sure you have appended characterEncoding=UTF-8 to connection-url in database service configuration file (For ex. mysql-ds.xml for mySQL database) as mentioned below.

<datasources>

<local-tx-datasource>

    <jndi-name>WSCDS</jndi-name>

   <connection-url>
   jdbc:mysql://{ipaddress}:{port}/{database_name}?characterEncoding=UTF-8
   </connection-url>

<driver-class>com.mysql.jdbc.Driver</driver-class>

<user-name>{username}</user-name>

<password>{password}</password>  

<metadata>

   <type-mapping>mySQL</type-mapping>

</metadata>

You need to use character encoding filter. Spring framework has a built-in character encoding filter. (see here) Simply put it in your 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>

Put this filter in the first place in filter chain to make sure it runs first during request processing and runs last during response processing.

You've mentioned, that you are not using any character filter. Please try adding one in web.xml. As far as I know, that is mandatory to be able to handle UTF-8 properly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!