Wednesday, October 29, 2014

To Take Foreign Language Input in JSF Applicaiton

The environment I use is the following: Tomcat 6, MySQL 5.5, JSF2 Facelets. The application needs to be able to allow the user to type in foreign characters such as Chinese characters and save them into the MySql database. To accomplish this, the following is the list of changes that will make it work. Some of them are actually not needed.
  1. The column that can take the input is "comment" in the table xyz. Its type is TEXT. Do the following to modify the character set of the column:
    alter table xyz modify comment text character set utf8;
    
  2. I use the file META-INF/context.xml for the datasource:
    <Context>
        <Manager pathname="" />
     <Resource name="jdbc/inventory" auth="Container" type="javax.sql.DataSource"
      url="jdbc:mysql://localhost:3306/yourDBName" 
      ...
         />
    </Context>
    
    Change the url to:
      url="jdbc:mysql://localhost:3306/yourDBName?useUnicode=true&amp;characterEncoding=UTF-8" 
    
  3. In the file C:\apache-tomcat-6.0.35\conf\server.xml, add a line URIEncoding="UTF-8" in the Connector configuration:
    <Connector port="8080" protocol="HTTP/1.1" 
                   connectionTimeout="20000" 
                   redirectPort="8443"
                   URIEncoding="UTF-8" />
    
    But this may not be needed.
  4. All my xhtml pages use a template. In the template file, there is the line
    <h:head>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
            ...
    </h:head>
    
    Change the charset in the line to charset=utf-8. But this is actually not needed.
  5. I use log4j.xml for the log file. Add a line for Encoding as below. This way the log file will not just show ? for the Chinese characters.
    <appender name="rollingFileSize" class="org.apache.log4j.RollingFileAppender">
      <param name="File" value="${logfile}" />
      <param name="MaxFileSize" value="20MB" />
      <param name="MaxBackupIndex" value="10" />
      <param name="Encoding" value="UTF-8" />
      ...
     </appender>
    
  6. In the faces-config.xml file, there is usually the following:
    <application>
        ...
        <locale-config>
         <default-locale>en</default-locale>
         <supported-locale>es</supported-locale>
        </locale-config>
     </application>
    
    You can add the following to support Chinese.
     <supported-locale>zh_CN</supported-locale>
    
    But it is not needed for the purpose to accept Chinese character input.

No comments:

Post a Comment