| |
java本身就支持多国语言编码,不需要写任何程序,可以很简单的 实现。 秘诀就是两点: 1、所有html/jsp页面全部采用utf-8编码 2、客户端浏览器完全支持utf-8编码 步骤: 1、首先把所有的html/jsp的contenttype都设为utf-8 2、然后对于jsp程序中的非ascii码提示信息都不应该写在程序里面,都应该放在 application.properties里面统一管理。 3、对html用native2ascii工具统一做一次处理,把html中的非ascii码都转换为unicode编码。 4、针对不同的语言,写不同的application.properties,比如说简体中文是 application_zh_cn.properties,繁体中文是application_zh_tw.properties这样,然后对这些配置信 息文件同样用native2ascii工具处理一次,把非ascii码统统转为unicode编码。 5、在servlet的request.getcharacterencoding()获得客户端的操作系统默认编码,然后set到struts 的httpsession的locale中。 ok!现在不同的客户访问,就会显示不同的语言版本了。你可以看看此时你的浏览器的字符集,就是 utf-8。现在你的网站和google一样了,嘿嘿,其实你有心的话,看看你的浏览器访问google的时候是 什么字符集吧 切记:所有的html/jsp都要设为utf-8编码,所有的文件中的非ascii码字符都要用native2ascii工具转 为用ascii表示的unicode编码。 -------------------------------------- 上面所述是我从网上下的一篇于中文问题的解决方案,确切的说应该是关于struts的国际化问题,下面我结合我的实践谈谈具体如何实现struts的国际化问题,我对理论不是非常精通,我只能完全凭自己的理解和实践来讲述,所以下面讲的内容可能不是非常正确,还请大家原谅。但有一点可以肯定,我通过自己的努力解决了struts的中文问题,并实现struts的国际化,其实一切并不复杂,下面是具体步骤: 0.遇到的问题(这些问题也许不会同时出现) a.中文数据从数据库中到jsp中后就变成了"????" b.做好的中文properties文件,其中的中文value在页面显示乱码 c.jsp文件中的中文到浏览器后显示时也是乱码(建议不要在jsp文件中输入中文,尽量放在properties文件中) d.由jsp传给bean的中文值,再由bean传回页面又是乱码 e.当更换本地浏览器的语言选项时,web应用程序不能自动根据你的locale选择合适的*.properties文件。导致web应用程序不能国际化。 1.环境: web服务器: tomcat 5.0.19 操作系统: win2000 server jvm : jdk 1.4 数 据 库: oracle 8.1.7 开发工具: struts studio 5.2 pro for eclipse 2.先将所有*.jsp 网页中开头处加入 再设置 3.然后编辑好两个*.properties文件,放在classes文件夹下你指定的地方,这里是放在/web-inf/classes/com/wiley 下,它们分别是: applicationresources.properties (英文资源文件) applicationresources_zh.properties (中文资源文件) 随便用什么工具编写都行啊! 4.将applicationresources_zh.properties转码成gb2312。上面引文说要转成utf-8,结果我试了,不行。转成gb2312就行了,操作是。 将applicationresources_zh.properties更名为applicationresources_xx.properties 在dos命令行进入applicationresources_xx.properties所在的文件夹 使用命令:native2ascii -encoding gb2312 applicationresources_xx.properties applicationresources_zh.properties(至于你为什么会出现“native2ascii不是内部命令”,,请查其它资料,可能你要设置环境变量,因为他是jdk的文件夹bin下的一个应用程序) 5.接下来配置struts-config.xml,很简单,我们加入: 就行了; 到此已能解决大多数中文问题。如上面所说的a,b,e 现在打开浏览器,选择菜单:工具》internet选项》语言,将“中文-中国[zh-cn]”删掉,添加一个“英语-英国[zh-gb]”确定后,重启tomcat,输入网址你就会发现,你的页面的文本信息就会用的是applicationresources.properties (英文资源文件)中的内容。如果换回“中文-中国[zh-cn]”,它就会显示applicationresources_zh.properties (中文资源文件)中的中文内容。 至于问题“c.jsp文件中的中文到浏览器后显示时也是乱码” 你就要用与第4步类似的方法来重新对*.jsp 文件编码,这时-encoding的参数就要用utf-8了,如果你用的也是struts studio 5.2 pro for eclipse工具,这一步就免了。它会自动用utf-8的格式存储。 至于问题“d.由jsp传给bean的中文值,再由bean传回页面又是乱码”的解决,我只是加了个过滤器。 你可以现在web.xml中加入: set character encoding com.wiley.setcharacterencodingfilter encoding utf-8 ignore true set character encoding action 然后在你指定的包内加个java文件 我放在了/web-inf/classes/com/wiley 里,下面是源代码: /* * xp forum * * copyright (c) 2002-2003 redsoft group. all rights reserved. * */ package com.huahang.tj.struts.filters; import javax.servlet.*; import java.io.ioexception; /** * 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 john wong * * @version $id: setcharacterencodingfilter.java,v 1.1 2002/04/10 13:59:27 johnwong exp $ */ 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
|
|