`
wangpan80
  • 浏览: 104774 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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中的例子。现将该例子的程序节选如下: 

  1. package filters;   
  2. import java.io.IOException;   
  3. import javax.servlet.Filter;   
  4. import javax.servlet.FilterChain;   
  5. import javax.servlet.FilterConfig;   
  6. import javax.servlet.ServletException;   
  7. import javax.servlet.ServletRequest;   
  8. import javax.servlet.ServletResponse;   
  9. import javax.servlet.UnavailableException;   
  10.   
  11. /**  
  12.  * <p>Example filter that sets the character encoding to be used in parsing the  
  13.  * incoming request, either unconditionally or only if the client did not  
  14.  * specify a character encoding.  Configuration of this filter is based on  
  15.  * the following initialization parameters:</p>  
  16.  * <ul>  
  17.  * <li><strong>encoding</strong> - The character encoding to be configured  
  18.  *     for this request, either conditionally or unconditionally based on  
  19.  *     the <code>ignore</code> initialization parameter.  This parameter  
  20.  *     is required, so there is no default.</li>  
  21.  * <li><strong>ignore</strong> - If set to "true", any character encoding  
  22.  *     specified by the client is ignored, and the value returned by the  
  23.  *     <code>selectEncoding()</code> method is set.  If set to "false,  
  24.  *     <code>selectEncoding()</code> is called <strong>only</strong> if the  
  25.  *     client has not already specified an encoding.  By default, this  
  26.  *     parameter is set to "true".</li>  
  27.  * </ul>  
  28.  *  
  29.  * <p>Although this filter can be used unchanged, it is also easy to  
  30.  * subclass it and make the <code>selectEncoding()</code> method more  
  31.  * intelligent about what encoding to choose, based on characteristics of  
  32.  * the incoming request (such as the values of the <code>Accept-Language</code>  
  33.  * and <code>User-Agent</code> headers, or a value stashed in the current  
  34.  * user's session.</p>  
  35.  *  
  36.  * @author Craig McClanahan  
  37.  * @version $Revision: 1.2 $ $Date: 2001/10/17 22:53:19 $  
  38.  */  
  39.   
  40. public class SetCharacterEncodingFilter implements Filter {   
  41.   
  42.   
  43.     // ----------------------------------------------------- Instance Variables   
  44.   
  45.   
  46.     /**  
  47.      * The default character encoding to set for requests that pass through  
  48.      * this filter.  
  49.      */  
  50.     protected String encoding = null;   
  51.   
  52.   
  53.     /**  
  54.      * The filter configuration object we are associated with.  If this value  
  55.      * is null, this filter instance is not currently configured.  
  56.      */  
  57.     protected FilterConfig filterConfig = null;   
  58.   
  59.   
  60.     /**  
  61.      * Should a character encoding specified by the client be ignored?  
  62.      */  
  63.     protected boolean ignore = true;   
  64.   
  65.   
  66.     // --------------------------------------------------------- Public Methods   
  67.   
  68.   
  69.     /**  
  70.      * Take this filter out of service.  
  71.      */  
  72.     public void destroy() {   
  73.   
  74.         this.encoding = null;   
  75.         this.filterConfig = null;   
  76.   
  77.     }   
  78.   
  79.   
  80.     /**  
  81.      * Select and set (if specified) the character encoding to be used to  
  82.      * interpret request parameters for this request.  
  83.      *  
  84.      * @param request The servlet request we are processing  
  85.      * @param result The servlet response we are creating  
  86.      * @param chain The filter chain we are processing  
  87.      *  
  88.      * @exception IOException if an input/output error occurs  
  89.      * @exception ServletException if a servlet error occurs  
  90.      */  
  91.     public void doFilter(ServletRequest request, ServletResponse response,   
  92.                          FilterChain chain)   
  93.         throws IOException, ServletException {   
  94.   
  95.         // Conditionally select and set the character encoding to be used   
  96.         if (ignore || (request.getCharacterEncoding() == null)) {   
  97.             String encoding = selectEncoding(request);   
  98.             if (encoding != null)   
  99.                 request.setCharacterEncoding(encoding);   
  100.         }   
  101.   
  102.         // Pass control on to the next filter   
  103.         chain.doFilter(request, response);   
  104.   
  105.     }   
  106.   
  107.   
  108.     /**  
  109.      * Place this filter into service.  
  110.      *  
  111.      * @param filterConfig The filter configuration object  
  112.      */  
  113.     public void init(FilterConfig filterConfig) throws ServletException {   
  114.   
  115.         this.filterConfig = filterConfig;   
  116.         this.encoding = filterConfig.getInitParameter("encoding");  
  117.         String value = filterConfig.getInitParameter("ignore");  
  118.         if (value == null)  
  119.             this.ignore = true;  
  120.         else if (value.equalsIgnoreCase("true"))  
  121.             this.ignore = true;  
  122.         else if (value.equalsIgnoreCase("yes"))   
  123.             this.ignore = true;   
  124.         else  
  125.             this.ignore = false;   
  126.   
  127.     }   
  128.   
  129.   
  130.     // ------------------------------------------------------ Protected Methods   
  131.   
  132.   
  133.     /**  
  134.      * Select an appropriate character encoding to be used, based on the  
  135.      * characteristics of the current request and/or filter initialization  
  136.      * parameters.  If no character encoding should be set, return  
  137.      * <code>null</code>.  
  138.      * <p>  
  139.      * The default implementation unconditionally returns the value configured  
  140.      * by the <strong>encoding</strong> initialization parameter for this  
  141.      * filter.  
  142.      *  
  143.      * @param request The servlet request we are processing  
  144.      */  
  145.     protected String selectEncoding(ServletRequest request) {   
  146.   
  147.         return (this.encoding);   
  148.   
  149.     }   
  150.   
  151. }  



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

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

  1. <filter>  
  2.     <filter-name>Set Character Encoding</filter-name>  
  3.     <filter-class>filters.SetCharacterEncodingFilter</filter-class>  
  4.     <init-param>  
  5.       <param-name>encoding</param-name>  
  6.       <param-value>UTF-8</param-value>  
  7.     </init-param>  
  8.   </filter>  
  9.   <filter-mapping>  
  10.     <filter-name>Set Character Encoding</filter-name>  
  11.     <url-pattern>/*</url-pattern>  
  12. </filter-mapping>  



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

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

  1. #Application Resource for the logon.jsp   
  2. logon.jsp.title=登录页   
  3. logon.jsp.page.heading=欢迎 世界!   
  4. logon.jsp.prompt.username=用户名:   
  5. logon.jsp.prompt.password=口令:   
  6. logon.jsp.prompt.submit=提交   
  7. logon.jsp.prompt.reset=复位   
  8.   
  9. #Application Resource for the main.jsp   
  10. main.jsp.title=主页   
  11. main.jsp.welcome=欢迎:   
  12.   
  13. #Application Resource for the LogonAction.java   
  14. error.missing.username=<li><font color="red">没有输入用户名</font></li>  
  15. error.missing.password=<li><font color="red">没有输入口令</font></li>  
  16.   
  17. #Application Resource for the UserInfoBo.java   
  18. error.noMatch=<li><font color="red">没有匹配的用户</font></li>  
  19.   
  20. #Application Resource for the UserInfoBo.java   
  21. error.logon.invalid=<li><font color="red">用户名/口令是无效的</font></li>  
  22. error.removed.user=<li><font color="red">找不到该用户</font></li>  
  23. error.unexpected=<li><font color="red">不可预期的错误</font></li>  



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

  1. #Application Resource for the logon.jsp   
  2. logon.jsp.title=\u767b\u5f55\u9875   
  3. logon.jsp.page.heading=\u6b22\u8fce \u4e16\u754c!   
  4. logon.jsp.prompt.username=\u7528\u6237\u540d:   
  5. logon.jsp.prompt.password=\u53e3\u4ee4:   
  6. logon.jsp.prompt.submit=\u63d0\u4ea4   
  7. logon.jsp.prompt.reset=\u590d\u4f4d   
  8.   
  9. #Application Resource for the main.jsp   
  10. main.jsp.title=\u4e3b\u9875   
  11. main.jsp.welcome=\u6b22\u8fce:   
  12.   
  13. #Application Resource for the LogonAction.java   
  14. error.missing.username=<li><font color="red">\u6ca1\u6709\u8f93\u5165\u7528\u6237\u540d</font></li>  
  15. error.missing.password=<li><font color="red">\u6ca1\u6709\u8f93\u5165\u53e3\u4ee4</font></li>  
  16.   
  17. #Application Resource for the UserInfoBo.java   
  18. error.noMatch=<li><font color="red">\u6ca1\u6709\u5339\u914d\u7684\u7528\u6237</font></li>  
  19.   
  20. #Application Resource for the UserInfoBo.java   
  21. error.logon.invalid=<li><font color="red">\u7528\u6237\u540d/\u53e3\u4ee4\u662f\u65e0\u6548\u7684</font></li>  
  22. error.removed.user=<li><font color="red">\u627e\u4e0d\u5230\u8be5\u7528\u6237</font></li>  
  23. error.unexpected=<li><font color="red">\u4e0d\u53ef\u9884\u671f\u7684\u9519\u8bef</font></li>  


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

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

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

参考文献:
《Programming Jakarta Struts》Chuck Cavaness著
《Mastering Jakarta Struts》James Goodwill著

分享到:
评论

相关推荐

    Struts原理与实践(罗会波)

    Struts原理与实践罗会波Struts原理与实践罗会波

    Struts原理与实践.mht

    Struts原理与实践(1) - Java - New - JavaEye论坛.mht

    Struts原理与实践

    Struts原理与实践,我也不想要大家的分,我实在是没分了。谢谢

    Struts原理与实践(二).rar

    Struts原理与实践(二).rar

    Struts原理与实践(一) .rar

    Struts原理与实践(一) .rar

    Struts原理与实践(五).rar

    Struts原理与实践(五).rar

    Struts原理与实践(三).rar

    Struts原理与实践(三).rar

    Struts+spring+hibernate学习笔记! - Struts原理与实践 - JavaEye知识库.files

    Struts+spring+hibernate学习笔记! - Struts原理与实践

    struts2 原理与实践

    struts2 原理与实践 值得收藏

    Struts原理与实践 ——

    下面,我们讨论一下最后一个问题,就是读写数据库时出现乱码。 现在一些常用的数据库都支持数据库encoding,也就是说在创建数据库时可以指定它自己的字符集设置, 数据库数据以指定的编码形式存储。...

    struts原理与实践+指导JAVA学习阶段需研究的开源项目

    主要讲解struts原理及实践应用和指导JAVA学习阶段需要研究的开源项目struts初学者及Java进阶学者

    struts原理与实践(一)

    很好的介绍struts的文章,自己就是一小节一小节学习的,每一小节都向目标迈进一步.所以将资源分了几个部分,这样大家学起来也比较有成就感.:) 本节介绍了struts的基本原理,流程图看起来很直接的哦..

    struts原理与实践(三)

    很好的介绍struts的文章,自己就是一小节一小节学习的,每一小节都向目标迈进一步.所以将资源分了几个部分,这样大家学起来也比较有成就感.:) 本节介绍了Struts JDBC的工作原理和数据库的连接配置..

    struts原理与实践二)

    很好的介绍struts的文章,自己就是一小节一小节学习的,每一小节都向目标迈进一步.所以将资源分了几个部分,这样大家学起来也比较有成就感.:) 本节介绍了Struts Web应用程序的开发步骤和一个简单例子的详细过程,...

    struts原理与实践(四)

    本节介绍了struts的国际化编程问题.. 很好的介绍struts的文章,自己就是一小节一小节学习的,每一小节都向目标迈进一步.所以将资源分了几个部分,这样大家学起来也比较有成就感.:)

    struts原理与实践(六)

    本节介绍了采用Struts中的输入校验问题.. 很好的介绍struts的文章,自己就是一小节一小节学习的,每一小节都向目标迈进一步.所以将资源分了几个部分,这样大家学起来也比较有成就感.:)

    struts原理与实践(七)

    本节介绍了如何实现一个文章发布系统.. 很好的介绍struts的文章,自己就是一小节一小节学习的,每一小节都向目标迈进一步.所以将资源分了几个部分,这样大家学起来也比较有成就感.:)

    初探Struts原理与实践.doc

    Struts作为一个开放原代码的应用框架,在最近几年得到了飞速的发展,在JSP Web应用开发中应用得非常广泛,有的文献上说它已经成为JSP Web应用框架的事实上的标准。那么,究竟什么是Struts呢?

Global site tag (gtag.js) - Google Analytics