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

后端 未结 3 1915
忘掉有多难
忘掉有多难 2021-01-16 02:27

I am building web application using

  1. Spring MVC
  2. Spring Security
  3. Hibenate
  4. MySQl

I want to add internationalization supp

相关标签:
3条回答
  • 2021-01-16 03:09

    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>
    
    0 讨论(0)
  • 2021-01-16 03:20

    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.

    0 讨论(0)
  • 2021-01-16 03:22

    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.

    0 讨论(0)
提交回复
热议问题