一,实现信息标记的本地化-利用ApplicationResources.properties文件管理页面的静态文本
1>配置struts-config.xml
<message-resources parameter="net.vicp.dogod.resources.ApplicationResources" />
2>建立两套资源文件,分别关联中文显示(zh_cn)和英文显示(en_us)
a)在net/vicp/dogod/resources文件夹下建立资源文件ApplicationResources.properties,这是默认的资源文件,其部分内容为:
--------
#errors
errors.userid.isBlank=userid is blank
errors.userid.notFound=cannot found this user
errors.password.isBlank=password is blank
errors.password.wrongPW=password is wrong
#Footer.jsp
Footer.copyright=Copyright@2006 Hawk&Dogod
Footer.tel=1234-12345678
Footer.eMail=hawk_dogod@126.com
#Login.jsp
Login.Hello=Welcome,
Login.userid= ID
Login.password=Password
---------------------
b)拷贝ApplicationResources.properties并修改文件名为ApplicationResources_en_US.properties,内容不变,此资源文件对应英文显示
c)拷贝ApplicationResources.properties并修改文件名为ApplicationResources_zh_CN_tmp.properties,这是个临时文件,作为转换成Unicode码的正式文件的过渡.其内容修改为:
--------
#errors
errors.userid.isBlank=未填登录名
errors.userid.notFound=未找到此用户
errors.password.isBlank=未填密码
errors.password.wrongPW=密码不匹配
#Footer.jsp
Footer.copyright=Copyright@2006 Hawk&Dogod
Footer.tel=1234-12345678
Footer.eMail=hawk_dogod@126.com
#Login.jsp
Login.Hello=欢迎
Login.userid=登录名
Login.password=密码
---------------------
使用jdk下的native2ascii工具将上面文件中的中文字符转换为ascii码,并生成一个最终使用的资源文件ApplicationResources_zh_CN.properties:
native2ascii使用方法:运行cmd,进入ApplicationResources_zh_CN_tmp.properties所在的文件夹,键入命令
native2ascii -encoding GBK ApplicationResources_zh_CN_tmp.properties ApplicationResources_zh_CN.properties
生成文件ApplicationResources_zh_CN.properties;其最终内容为:
----------------------
#errors
errors.userid.isBlank=/u672a/u586b/u767b/u5f55/u540d
errors.userid.notFound=/u672a/u627e/u5230/u6b64/u7528/u6237
errors.password.isBlank=/u672a/u586b/u5bc6/u7801
errors.password.wrongPW=/u5bc6/u7801/u4e0d/u5339/u914d
#Footer.jsp
Footer.copyright=Copyright@2006 Hawk&Dogod
Footer.tel=1234-12345678
Footer.eMail=hawk_dogod@126.com
#Login.jsp
Login.Hello=/u6b22/u8fce
Login.userid=/u767b/u5f55/u540d
Login.password=/u5bc6/u7801
---------------------------
3>测试效果:
写一个测试页-TestLocal.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<html:base />
<title>TestLocal.jsp</title>
</head>
<body>
<bean:message key="Login.Hello"/>
</body>
</html:html>
在浏览器的"工具"--"Internet选项"的"语言首选项"对话框中,分别选择 英语(美国) [en-us]和中文(中国)[zh-cn],试登录TestLocal.jsp页,此时,就会按照浏览器的设定的语言显示。也就是说不同国家(地区)的客户都可以看到自己语言的内容,这就简单实现了国际化编程的基本要求。如果还要显示其他语言,可采用类似方法进行,如西班牙语(秘鲁)[es-pe] 的资源文件名就应为ApplicationResources_es_pe.properties
二,数据传输中的字符集设置-通过过滤器SetCharacterEncodingFilter类设置应答过程中的字符集
1>/WEB-INF/classes/filters目录新建SetCharacterEncodingFilter类,其代码为
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
/**
* <p>Example filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user's session.</p>
*
* @author Craig McClanahan
* @version $Revision: 267129 $ $Date: 2004-03-18 11:40:35 -0500 (Thu, 18 Mar 2004) $
*/
public class SetCharacterEncodingFilter implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// ------------------------------------------------------ Protected Methods
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
此类可以从tomcat 的实例中直接拷贝,代码无需修改,这是个完整的过滤器实现
2>配置/WEB-INF/web.xml,为当前web应用配置此过滤器,
web.xml部分相关代码为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>
Java_Struts Project
</description>
<display-name>J42</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- a filter-->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
<!-- filter End-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
同过以上两步基本可解决本地化问题