Struts原理与实践(五)

使用Struts实现应用程序国际化编程
本文介绍支持i18n的应用程序特征,阐述Struts采用消息类和JSP定制标签两个i18n组件实现国际化编程。还说明了用Struts实现国际化的标准做法,包括生成属性文件集、设置字符集、使用filter及准备资源包文件等,最终实现不同语言显示。

一个支持i18n的应用程序应该有如下一些特征:
1增加支持的语言时要求不更改程序代码
2字符元素、消息、和图象保存在原代码之外
3依赖于不同文化的数据如:日期时间、小数、及现金符号等数据对用户的语言和地理位置应该有正确的格式
4应用程序能迅速地适应新语言和/或新地区

Struts主要采用两个i18n组件来实现国际化编程:

第一个组件是一个被应用程序控制器管理的消息类,它引用包含地区相关信息串的资源包。第二个组件是一个JSP定制标签,<?XML:NAMESPACE PREFIX = BEAN /><message></message>,它用于在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中的例子。现将该例子的程序节选如下:

<ccid_nobr><p> </p> <table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1"><tbody><tr><td class="code" style="FONT-SIZE: 9pt" bgcolor="#e6e6e6"><pre><ccid_code><font color="#a52a2a">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; /** * &lt;p&gt;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:&lt;/p&gt; * &lt;ul&gt; * &lt;li&gt;&lt;strong&gt;encoding&lt;/strong&gt; - The character encoding to be configured * for this request, either conditionally or unconditionally based on * the &lt;code&gt;ignore&lt;/code&gt; initialization parameter. This parameter * is required, so there is no default.&lt;/li&gt; * &lt;li&gt;&lt;strong&gt;ignore&lt;/strong&gt; - If set to "true", any character encoding * specified by the client is ignored, and the value returned by the * &lt;code&gt;selectEncoding()&lt;/code&gt; method is set. If set to "false, * &lt;code&gt;selectEncoding()&lt;/code&gt; is called &lt;strong&gt;only&lt;/strong&gt; if the * client has not already specified an encoding. By default, this * parameter is set to "true".&lt;/li&gt; * &lt;/ul&gt; * * &lt;p&gt;Although this filter can be used unchanged, it is also easy to * subclass it and make the &lt;code&gt;selectEncoding()&lt;/code&gt; method more * intelligent about what encoding to choose, based on characteristics of * the incoming request (such as the values of the &lt;code&gt;Accept-Language&lt;/code&gt; * and &lt;code&gt;User-Agent&lt;/code&gt; headers, or a value stashed in the current * user's session.&lt;/p&gt; * * @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 * &lt;code&gt;null&lt;/code&gt;. * &lt;p&gt; * The default implementation unconditionally returns the value configured * by the &lt;strong&gt;encoding&lt;/strong&gt; initialization parameter for this * filter. * * @param request The servlet request we are processing */ protected String selectEncoding(ServletRequest request) { return (this.encoding); } }</font></ccid_code></pre></td></tr></tbody></table></ccid_nobr>



其中,request.setCharacterEncoding(encoding);是一个关键句子。

为了让该类工作,我们还要在web.xml文件中对它进行配置,配置代码如下:

<ccid_nobr><p> </p> <table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1"><tbody><tr><td class="code" style="FONT-SIZE: 9pt" bgcolor="#e6e6e6"><pre><ccid_code><font color="#a52a2a">&lt;filter&gt; &lt;filter-name&gt;Set Character Encoding&lt;/filter-name&gt; &lt;filter-class&gt;filters.SetCharacterEncodingFilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;encoding&lt;/param-name&gt; &lt;param-value&gt;UTF-8&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;Set Character Encoding&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt;</font></ccid_code></pre></td></tr></tbody></table></ccid_nobr>



最后,就是准备资源包文件,我们以创建一个中文文件为例:

将ApplicationResources.properties文件打开,另存为ApplicationResources_zh.properties,这只是一个过渡性质的文件。将文件中键/值对的值都用中文表示。更改完后的代码如下:

<ccid_nobr><p> </p> <table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1"><tbody><tr><td class="code" style="FONT-SIZE: 9pt" bgcolor="#e6e6e6"><pre><ccid_code><font color="#a52a2a">#Application Resource for the logon.jsp logon.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.jsp main.jsp.title=主页 main.jsp.welcome=欢迎: #Application Resource for the LogonAction.java error.missing.username=&lt;li&gt;&lt;font color="red"&gt;没有输入用户名&lt;/font&gt;&lt;/li&gt; error.missing.password=&lt;li&gt;&lt;font color="red"&gt;没有输入口令&lt;/font&gt;&lt;/li&gt; #Application Resource for the UserInfoBo.java error.noMatch=&lt;li&gt;&lt;font color="red"&gt;没有匹配的用户&lt;/font&gt;&lt;/li&gt; #Application Resource for the UserInfoBo.java error.logon.invalid=&lt;li&gt;&lt;font color="red"&gt;用户名/口令是无效的&lt;/font&gt;&lt;/li&gt; error.removed.user=&lt;li&gt;&lt;font color="red"&gt;找不到该用户&lt;/font&gt;&lt;/li&gt; error.unexpected=&lt;li&gt;&lt;font color="red"&gt;不可预期的错误&lt;/font&gt;&lt;/li&gt;</font></ccid_code></pre></td></tr></tbody></table></ccid_nobr>



使用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的内容如下:

<ccid_nobr><p> </p> <table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1"><tbody><tr><td class="code" style="FONT-SIZE: 9pt" bgcolor="#e6e6e6"><pre><ccid_code><font color="#a52a2a">#Application Resource for the logon.jsp logon.jsp.title=\u767b\u5f55\u9875 logon.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\u4ea4 logon.jsp.prompt.reset=\u590d\u4f4d #Application Resource for the main.jsp main.jsp.title=\u4e3b\u9875 main.jsp.welcome=\u6b22\u8fce: #Application Resource for the LogonAction.java error.missing.username=&lt;li&gt;&lt;font color="red"&gt;\u6ca1\u6709\u8f93\u5165\u7528\u6237\u540d&lt;/font&gt;&lt;/li&gt; error.missing.password=&lt;li&gt;&lt;font color="red"&gt;\u6ca1\u6709\u8f93\u5165\u53e3\u4ee4&lt;/font&gt;&lt;/li&gt; #Application Resource for the UserInfoBo.java error.noMatch=&lt;li&gt;&lt;font color="red"&gt;\u6ca1\u6709\u5339\u914d\u7684\u7528\u6237&lt;/font&gt;&lt;/li&gt; #Application Resource for the UserInfoBo.java error.logon.invalid=&lt;li&gt;&lt;font color="red"&gt;\u7528\u6237\u540d/\u53e3\u4ee4\u662f\u65e0\u6548\u7684&lt;/font&gt;&lt;/li&gt; error.removed.user=&lt;li&gt;&lt;font color="red"&gt;\u627e\u4e0d\u5230\u8be5\u7528\u6237&lt;/font&gt;&lt;/li&gt; error.unexpected=&lt;li&gt;&lt;font color="red"&gt;\u4e0d\u53ef\u9884\u671f\u7684\u9519\u8bef&lt;/font&gt;&lt;/li&gt;</font></ccid_code></pre></td></tr></tbody></table></ccid_nobr>



从这里可以看出,所有的中文字都转换成了对应的Unicode码。

现在,再运行登录例子程序,您会发现它已经是显示的中文了。在浏览器的"工具"--"Internet选项"的"语言首选项"对话框中,去掉"中文(中国)"加上英文,再试登录程序,此时,又会显示英文。这就是说不同国家(地区)的客户都可以看到自己语言的内容,这就实现了国际化编程的基本要求。如果还要显示其他语言,可采用类似处理中文的方法进行,这里就不细讲了。

本文中的例子程序所采用的数据库仍然是MS SQLServer2000,数据库字符集为gbk。实验表明,对简、繁体中文,英文及日文字符都能支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值