feng_sundy 的专栏

网站建设,深圳网站建设,网页设计深圳,平面设计,深圳平面设计,深圳多媒体设计

原创 "struts中文问题","struts国际化问题"——终极解决方案收藏

引文
----------------------------------------
----------------------------------------
说实话,你们的方法都做的复杂了,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 网页中开头处加入

    <%@ page language="java" contentType="text/html; charset=utf-8" %>
     再设置<html:html locale = "true">

  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,很简单,我们加入:

   <message-resources parameter="com.wiley.ApplicationResources"/>  就行了;

   到此已能解决大多数中文问题。如上面所说的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中加入:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.wiley.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>

  然后在你指定的包内加个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;

/**
* <p>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 <a href="mailto:jwtronics@yahoo.com">John Wong</a>
*
* @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 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);

}

}//EOC
   到此我遇到的中文问题已全部得到解决,并从中理解到struts的国际化的深刻含义。
我个人觉得struts作为一个功能强大的应用框架,应该早就考虑到它的国际化问题,并在实际应用中不会很复杂,只要我们遵循一些规则,就可以尽情享受struts给我们带来的无穷乐趣。希望以上所述对大家有所帮助。

发表于 @ 2004年10月17日 11:20:00|评论(loading...)

新一篇: 解读多重启动引导文件——BOOT.INI | 旧一篇: 使用JSP面向对象web编程技术实现树形控件

用户操作
[即时聊天] [发私信] [加为好友]
feng_sundy
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
feng_sundy的公告
最近太多垃圾评论,暂时关闭评论,有问题,Email联系或者留言。
如有转载敬请注明出处。版权所有
feng_sundy © 2004-2008
sundysea@hotmail.com
sundysea@gmail.com
\ \ \ \ \
文章分类
收藏
DHTML & CSS
CSS alistapart.com
DHTMLGoodies
dudo博客
FCKEditor
Google Code
Google Web Toolkit
HTML Goodies
HTML.it
HTMLAre
HTMLdog
http://developer.yahoo.com/
JavaScript Kit
JQuery
KindEditor
Prototype
Tab Menu(exploding-boy.com)
TinyMCE
VBScript RegExp Object
W3 Schools
Yahoo JavaScript Developer Center
Yahoo! UI Library (YUI)
摄氏零度以后Jim's Blog
正则表达式
网页设计师Web标准
Javascript
Datejs.com
Drag drop JS
javascript.about.
leigeber.com
w3school
Java代码
codeproject.com
Eclipse Web Tools
Java.util
Java图像处理类库
Java处理Gif图片
Java学习源代码检索中心
Java开源大全
subversion
JSON VS XML
www.json.org
Mysql
Mysql Blog
N人Blog
idv2.com
meizz
nightsailer
Rubic's Cube Solution
Script.aculo.us开发系列
涤心斋
Prototype[script.aculo.us]
电影
MTime
tom365
软件下载
windows系统优化
心海e站
网站素材
IT墨水瓶
站酷(ZCOOL)_酷站
文档分享
分享文档-分享你我的Java文档
友情链接
feng ye
JavaScript tutorial and free code scripts
Java之路
Linux技术中坚站论坛
LVS中文站点
lxgljj的专栏
Movies手册
MyJsFrame
Windows Live
专注WEB开发
中国Linux公社
分享Java快乐
注册码大全
泽欧里 Zeal Li
老康之家
车东 Che, Dong
键者天行
阿赖的个性空间(RSS)
雕刻时光
黑夜路人的开源世界
自助建站
『更新易網』
【商贸易】系统
Affiliate Program Center
Builder spot
e Biz Webpages
Hishop 电子商务平台
Mambo Server
Mambo Server
Mambo中国
Members Section
Online web site builder
oscommerce
osCommerce Templates
ShopEx
SWsoft
UltraCMS
Web Design Library
Web2Go自動網頁
WebCortex
乔客在线
大中华商贸网
梦想家网店系统
存档
Csdn Blog version 3.1a
Copyright © feng_sundy