struts 原理与实践(5)

来源:百度文库 编辑:神马文学网 时间:2024/06/03 13:49:13
Struts 原理与实践(5) - -
一个支持i18n的应用程序应该有如下一些特征:
1增加支持的语言时要求不更改程序代码
2字符元素、消息、和图象保存在原代码之外
3依赖于不同文化的数据如:日期时间、小数、及现金符号等数据对用户的语言和地理位置应该有正确的格式
4应用程序能迅速地适应新语言和/或新地区
Struts主要采用两个i18n组件来实现国际化编程:
第一个组件是一个被应用程序控制器管理的消息类,它引用包含地区相关信息串的资源包。第二个组件是一个JSP定制标签,,它用于在View层呈现被控制器管理的实际的字符串。在我们前面的登录例子中这两方面的内容都出现过。
用Struts实现国际化编程的标准做法是:生成一个java属性文件集。每个文件包含您的应用程序要显示的所有消息的键/值对。
这些文件的命名要遵守如下规则,代表英文消息的文件可作为缺省的文件,它的名称是ApplicationResources.properties;其他语种的文件在文件名中都要带上相应的地区和语言编码串,如代表中文的文件名应为ApplicationResources_zh_CN.properties。并且其他语种的文件与ApplicationResources.properties文件要放在同一目录中。
ApplicationResources.properties文件的键/值都是英文的,而其他语种文件的键是英文的,值则是对应的语言。如在我们前面的登录例子中的键/值对:logon.jsp.prompt.username=Username:在中文文件中就是:logon.jsp.prompt.username=用户名:当然,在实际应用时要把中文转换为AscII码。
有了上一篇文章和以上介绍的一些基础知识后。我们就可以将我们的登录程序进行国际化编程了。
首先,我们所有jsp页面文件的字符集都设置为UTF-8。即在页面文件的开始写如下指令行:
,在我们的登录例子中已经这样做了,这里不需要再改动。
其次,将所有的request的字符集也设置为UTF-8。虽然,我们可以在每个文件中加入这样的句子:request.setCharacterEncoding("UTF-8");来解决,但这样显得很麻烦。一种更简单的解决方法是使用filter。具体步骤如下:
在mystruts\WEB-INF\classes目录下再新建一个名为filters的目录,新建一个名为:SetCharacterEncodingFilter的类,并保存在该目录下。其实,这个类并不要您亲自来写,可以借用tomcat中的例子。现将该例子的程序节选如下:
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;/** *
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:
* * encoding - The character encoding to be configured * for this request, either conditionally or unconditionally based on * the ignore initialization parameter. This parameter * is required, so there is no default. * ignore - If set to "true", any character encoding * specified by the client is ignored, and the value returned by the * selectEncoding() method is set. If set to "false, * selectEncoding() is called only if the * client has not already specified an encoding. By default, this * parameter is set to "true". *
* *
Although this filter can be used unchanged, it is also easy to * subclass it and make the selectEncoding() method more * intelligent about what encoding to choose, based on characteristics of * the incoming request (such as the values of the Accept-Language * and User-Agent headers, or a value stashed in the current * user‘s session.
* * @author Craig McClanahan * @version $Revision: 1.2 $ $Date: 2001/10/17 22:53:19 $ */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 * null. *
* The default implementation unconditionally returns the value configured * by the encoding initialization parameter for this * filter. * * @param request The servlet request we are processing */ protected String selectEncoding(ServletRequest request) { return (this.encoding); }}
其中,request.setCharacterEncoding(encoding);是一个关键句子。
为了让该类工作,我们还要在web.xml文件中对它进行配置,配置代码如下:
Set Character Encoding filters.SetCharacterEncodingFilter encoding UTF-8 Set Character Encoding /*
最后,就是准备资源包文件,我们以创建一个中文文件为例:
将ApplicationResources.properties文件打开,另存为ApplicationResources_zh.properties,这只是一个过渡性质的文件。将文件中键/值对的值都用中文表示。更改完后的代码如下:
#Application Resource for the logon.jsplogon.jsp.title=登录页logon.jsp.page.heading=欢迎 世界!logon.jsp.prompt.username=用户名:logon.jsp.prompt.password=口令:logon.jsp.prompt.submit=提交logon.jsp.prompt.reset=复位#Application Resource for the main.jspmain.jsp.title=主页main.jsp.welcome=欢迎:#Application Resource for the LogonAction.javaerror.missing.username=没有输入用户名error.missing.password=没有输入口令#Application Resource for the UserInfoBo.javaerror.noMatch=没有匹配的用户#Application Resource for the UserInfoBo.javaerror.logon.invalid=用户名/口令是无效的error.removed.user=找不到该用户error.unexpected=不可预期的错误
使用native2ascii工具将上面文件中的中文字符转换为ascii码,并生成一个最终使用的资源文件ApplicationResources_zh_CN.properties。
具体做法是打开一个dos窗口,到mystruts\WEB-INF\classes目录下,运行如下语句:
native2ascii -encoding GBK ApplicationResources_zh.properties ApplicationResources_zh_CN.properties
生成的文件ApplicationResources_zh_CN.properties的内容如下:
#Application Resource for the logon.jsplogon.jsp.title=\u767b\u5f55\u9875logon.jsp.page.heading=\u6b22\u8fce \u4e16\u754c!logon.jsp.prompt.username=\u7528\u6237\u540d:logon.jsp.prompt.password=\u53e3\u4ee4:logon.jsp.prompt.submit=\u63d0\u4ea4logon.jsp.prompt.reset=\u590d\u4f4d#Application Resource for the main.jspmain.jsp.title=\u4e3b\u9875main.jsp.welcome=\u6b22\u8fce:#Application Resource for the LogonAction.javaerror.missing.username=\u6ca1\u6709\u8f93\u5165\u7528\u6237\u540derror.missing.password=\u6ca1\u6709\u8f93\u5165\u53e3\u4ee4#Application Resource for the UserInfoBo.javaerror.noMatch=\u6ca1\u6709\u5339\u914d\u7684\u7528\u6237#Application Resource for the UserInfoBo.javaerror.logon.invalid=\u7528\u6237\u540d/\u53e3\u4ee4\u662f\u65e0\u6548\u7684error.removed.user=\u627e\u4e0d\u5230\u8be5\u7528\u6237error.unexpected=\u4e0d\u53ef\u9884\u671f\u7684\u9519\u8bef
从这里可以看出,所有的中文字都转换成了对应的Unicode码。
现在,再运行登录例子程序,您会发现它已经是显示的中文了。在浏览器的"工具"--"Internet选项"的"语言首选项"对话框中,去掉"中文(中国)"加上英文,再试登录程序,此时,又会显示英文。这就是说不同国家(地区)的客户都可以看到自己语言的内容,这就实现了国际化编程的基本要求。如果还要显示其他语言,可采用类似处理中文的方法进行,这里就不细讲了。
本文中的例子程序所采用的数据库仍然是MS SQLServer2000,数据库字符集为gbk。实验表明,对简、繁体中文,英文及日文字符都能支持。
参考文献:
《Programming Jakarta Struts》Chuck Cavaness著
《Mastering Jakarta Struts》James Goodwill著