随笔-40  评论-66  文章-0  trackbacks-0
 

java 中文乱码处理。

 
参考
http://china.eceel.com/article/study_for_character_encoding_java.htm
http://upurban.com/bbs/viewtopic.php?t=246

1。什么是utf-8,什么是ISO-8859-1,什么是GB2312,还有什么是unicode

2。java 程序的字符的表示格式

3。jsp 程序中文显示处理实例

3。1
<%@ page  pageEncoding="ISO-8859-1"%>和<%@ page  pageEncoding="GB2312"%>和<%@ page 

pageEncoding="UTF-8"%>各自的意思是什么,他们是否只对post提交有效!
request.setCharacterEncoding("UTF-8")是什么意思?有什么区别?还有

response.setCharacterEncoding("UTF-8"),优先于下边
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

setCharacterEncoding()该函数用来设置http请求或者相应的编码。

对于request,是指提交内容的编码,指定后可以通过getParameter()则直接获得正确的字符串,如果不

指定,则默认使用iso8859-1编码,需要进一步处理。参见下述"表单输入"。值得注意的是在执行

setCharacterEncoding()之前,不能执行任何getParameter()。java doc上说明:This method must be

called prior to reading request parameters or reading input using getReader()。而且,该指定

只对POST方法有效,对GET方法无效。分析原因,应该是在执行第一个getParameter()的时候,java将会

按照编码分析所有的提交内容,而后续的getParameter()不再进行分析,所以setCharacterEncoding()无

效。而对于GET方法提交表单是,提交的内容在URL中,一开始就已经按照编码分析所有的提交内容,

setCharacterEncoding()自然就无效。

对于response,则是指定输出内容的编码,同时,该设置会传递给浏览器,告诉浏览器输出内容所采用的

编码。
 
3.2. jsp输出

指定文件输出到browser是使用的编码,该设置也应该置于文件的开头。例如:<%@ page

contentType="text/html; charset= GBK" %>。该设置和response.setCharacterEncoding("GBK")等效。

 

4。java EE程序利用过滤器 处理中文问实例
提交数据的编码格式
tomcat默认提交格式是ISO-8859-1
可以通过设置过滤器(只针对post提交)或修改server.xml 的URIencoding 编码格式(只针对get提交)

达到你想要的 数据提交编码格式。

 

总结

---by mylu 18:26 2007-5-20

posted @ 2007-05-20 22:36 Super·shen BLOG 阅读(1512) | 评论 (0)编辑 收藏

ORM
Object Relation Mapping
对象 关系 映射

对象 指实体域对象
关系 关系数据


模型

概念模型(实体-属性)
关系数据模型(关系数据库)
域模型(对象)


软件分层

v - 表述层
c /

    /业务层
m- 持久层(hibernate 技术实现)
    \数据层


mvc 对应 各层次


概念实体关系

1对1
1对多
多对多


表与表之间的关系 参照完整性

外键
多对多
多对一


域对象之间的 关系

关联 (一对一 一对多 多对多)
依赖 (一个类需要访问另外一个类)
聚集 (一个类的对象是另一个类的一部分, 人和手)
一般化 (继承关系)


域对象
 实体域对象  (实体EJB,POJO)
过程域对象  (会话EJB,消息驱动EJB,POJO)
事件域对象  ()

在hibernate中 一般只关注 实体域对象 和 过程域对象


域对象的关系

 域对象的关联关系 是有方向的
体现在类的编码不一样的

单向关联
双向关联

 


域对象的持久化
把对象从内存中 保存到持久化设备中去

ORM 与  ORM模式
ORM模式是一种持久化技术,还有其他模式的持久化技术。如主动域模式(BMP),JDO模式,CMP模式。


域模型和数据模型的各个不匹配之处
1,继承
2,多对多
3,双向
4。粒度
尽量少连接查询,很消耗时间的操作

 


创建持久化类


1。持久化类符合javabean的规范,包含一些属性 以及对应的getxxx 色天下学习方法
2。持久化类有一个id属性,用来唯一表示类的每一个对象。 也叫OID 对象表示符
3。Hibernate要求持久化类必须提供一个不带参数的默认构造方法

创建数据库schema

创建对象-关系映射文件

(一般在eslispe中先创建数据库 然后再创建持久化类以及映射文件)

hibernate 映射类型


hibernate的初始化

static{

try{
//根据默认位置的hibernate配置文件创建 configuration实例
Configuration config = new Configuration();
config.addClass(Customer.class);
//创建SessionFactory 实例
sessionFactory = config.buildSessinoFactroy();
}catch(Exception e){e.printStackTrace();}
}


SessionFactory 接口

一个SessionFactory 实例是对应一个数据源的,应用从SessionFactory 获取session实例对象
1线程安全的
2重量级的,不能随意创建和销毁她的实例。

Session 接口

1 Session接口是hibernate应用最为广泛的接口。
2 Session也被称为持久化管理器,它提供和持久化相关的操作
3 Session有以下特点
 a 不是线程安全的 所以应避免多线程共用一个Session实例
 b Session实例是轻量级的,所谓轻量级是指他的创建和销毁不需要消耗太多的资源。意味着程序中可以经常创建和销毁Session实例,保证不多线程使用Session对象。

Session接口的常用方法:
save()
update()
delete()
load()

Session执行事务流程

Session session = factory.openSession();
Transaction tx;
try{
tx = session.beginTranscation();
//执行事务
...
//提交事务
tx.commit();
}
catche(Exception e)
{//如果出现异常,撤消事务
if(tx!=null)tx.rollback();
throw e;
}
finally{
session.close(); //不管事务是否成功,最后都要关闭session对象
}
}

 

 

 

 

posted @ 2007-02-07 14:32 Super·shen BLOG 阅读(398) | 评论 (0)编辑 收藏

eXtremeComponents FAQ

eXtremeComponents FAQ(中文版)

Jeff Johnston

Lucky

冷月宫主

版本0.1.0

本文档允许在遵守以下两条原则的条件下被使用和传播: 1)不能凭借本文档索取任何费用 2)以任何方式(印刷物或电子版)使用和传播时本文档时,必须包含本版权申明

(更新中...)


eXtremeComponents FAQ(中文)


1.?如何使用导出功能

Q: 如何使用导出功能

A: 为了使用导出功能,只需要在web.xml文件中加入eXtremeComponents的导出过滤器的配置,内容如下:

<filter>
<filter-name>eXtremeExport</filter-name>
<filter-class>org.extremecomponents.table.filter.ExportFilter</filter-class>
<init-param>
<param-name>responseHeadersSetBeforeDoFilter</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>eXtremeExport</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2.?传入中文参数乱码

Q: 传入中文参数乱码,如下页面:

		<form id="form1" name="form1" method="post" action="应用eXtremeTable的action或是结果页面名">
<select name="selecttype" size="6">
<option value="第一个">第一个</option>
<option value="第二个">第二个</option>
<option value="第三个">第三个</option>
</select>
<input type="text" name="username" />
<input type="submit" name="Submit" value="提交" />
</form>

当你提交时含有eXtremeTable的结果页面会自动取得页面上的表单参数,那怕是经过了action的mapping.findForward("forward"),在我的试用过程中到页面上会出现传递过去的参数,但出现了乱码问题,使用查询(filter)功能是的中文参数问题类似。

A:

  1. 确认服务器的参数是否设置了正确的编码,如果使用Tomcat请确认Server.xml:

     <Connector port="80" URIEncoding="UTF-8" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false"
    redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" />
  2. 添加编码过滤器到你的应用工程:

    /*
    * Copyright 1999-2001,2004 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */


    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;


    /**
    * <p>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:</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 Craig McClanahan
    * @version $Revision: 1.3 $ $Date: 2004/02/28 03:35:22 $
    */

    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);

    }


    }
  3. 在web.xml中添加编码过滤器配置:

     <filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>filters.SetCharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>gb2312</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>Set Character Encoding</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

3.?导出时中文文件名乱码

Q:关于导出时中文文件名为乱码的问题

A: 这是个bug,建议使用英文文件名,主要原因还是编码问题。我们现在正在想办法解决。

4.?导出时文件内容乱码

Q:导出时文件内容乱码

A:首先请确认使用的是extremecomponents-1.0.1-M5-A4版以后的版本

  1. Excle: 导出为Excle的中文问题已经修正,默认的情况下支持导出中文,用户不需要任何改动
  2. PDF : 由于extremecomponents使用了FOP来生成PDF文件,FOP在导出中文内容时会产生乱码。具体的解决方案 大家可以参考最新eXtremeComponents包:支持 PDF中文导出

5.?变量命名问题

Q:当变量名为"action",在IE下执行产生javascript错误

A: 内部使用了一些关键字,就目前我所知的为"action"、"submit"。建议大家命名时尽量避免,如果大家必须使用,则可以使用table标签的autoIncludeParameters参数设置为"false":

autoIncludeParameters="false"

6.?格式化输出表单中的数据

Q:怎么样格式化输出表单中的数据

A: 你可以设置列的cell:

  1. 日期格式化: cell = " date " format = " yyyy-MM-dd "
  2. 数字格式化: cell="currency" format="###,###,##0.00"

详细信息请参考指南

7.?加入链接

Q:怎么样加入链接

A: 你可以参考下例:

            <ec:table
var="pres"
items="presidents"
action="${pageContext.request.contextPath}/compact.run"
imagePath="${pageContext.request.contextPath}/images/table/compact/*.gif"
view="compact"
title="Compact Toolbar View"
showTooltips="false"
>
<ec:exportPdf
fileName="output.pdf"
tooltip="Export PDF"
headerColor="black"
headerBackgroundColor="#b6c2da"
headerTitle="Presidents"
text="PDF"
/>
<ec:exportXls
fileName="output.xls"
tooltip="Export Excel"
text="XLS"
/>
<ec:row>
<ec:column property="fullName" title="Name">
<a href="http://www.whitehouse.gov/history/presidents/">${pres.fullName}</a>
</ec:column>
<ec:column property="nickName"/>
<ec:column property="term"/>
<ec:column property="born" cell="date"/>
<ec:column property="died" cell="date"/>
<ec:column property="career"/>
</ec:row>
</ec:table>

8.?行高亮显示

Q: 我想使用行的高亮显示如何设置

A: 你只需要设置行标签的highlightRow属性: highlightRow="true"。eXtremeComponents提供了很多接口允许用户按照自己的习惯来进行定制,包括:CSS、CELL、View。相关信息请参考指南。



by lucky
posted @ 2007-01-26 16:04 Super·shen BLOG 阅读(273) | 评论 (0)编辑 收藏
》对象之间方法调用,通过传递消息

OOP使各个对象各司其职,分别负担执行一组相关的任务,如果一个对象要依赖于一个不在其范围内的方法,它就需要访问包含该方法的第二个对象,即第一个对象需要第二个对象执行这个方法(或者叫方法调用) 利用OOP术语,叫做一个对象向另外一个对象发送消息。

》对象的生成: 对象是在执行过程中由其所属的类动态生成的。 一个类可以生成多个不同的对象。


》 消息与方法的概念

对象之间的传递通过消息传递完成

一个发送消息的对象 发送的消息包含3个方面的内容
1,接受消息的对象
2,接受对象应用的方法。
3,方法所需要的参数。

》面向对象变成的基本特征
1 封装性 Encapsulation 把数据和操作组织在类内
2 继承性 Inheritance 通过类的继承关系
3多态性Polymophism(在运行的时候体现)   A通过方法重裁 B通过方法重写,子类覆盖父类的方法(接口一个种特殊的类哦)

posted @ 2007-01-24 17:47 Super·shen BLOG 阅读(346) | 评论 (0)编辑 收藏
域对象之间的4种关系

1。关联

指类之间的引用关系,是实体域对象的最普遍的关系。
一对一、一对多、多对多关联

如果类A和类B关联,那么被引用的B将被定义为A的属性。

Customer  与 Order 是一对多的关联关系

Order 类 包含 Customer类的 属性

Customer  类 包含  集合 Order 


2。依赖

posted @ 2006-12-30 16:30 Super·shen BLOG 阅读(235) | 评论 (0)编辑 收藏


一个J2EE 工程中涉及的对象

  • 数据传输对象DTO
  • 业务对象BO (实体业务对象 过程业务对象 事件业务对象)
  • 数据访问对象DAO

 


概念

持久化框架、ORM框架、DAO设计模式

他们的关系是:ORM框架是一种持久化框架,DAO是用于实现持久化框架的一种设计模式。

posted @ 2006-12-27 17:14 Super·shen BLOG 阅读(398) | 评论 (0)编辑 收藏
 ActionErrors   errors   =   null;    
  errors   =   new   ActionErrors();    
  errors.add("isExist",   new   ActionError("error.isExist"));   
// errors.add("isExist",   new   ActionError("error.isExist"));   等效于errors.add("isExist",   new   ActionMessage("error.isExist"));    

  saveErrors(request,   errors);  
  return   (mapping.findForward("failure"));   


failure页面里也定义了<html:errors   name="isExist"/>  
  ApplicationResources.properties里面也定义了error.isExist=user   have   already   exist!!!   
  运行结果 跳转到failure页面,显示“user   have   already   exist!!!   ”




ActionErrors.GLOBAL_ERROR
怎么理解
它和我们使用普通的字符有什么区别啊

部分代码如下:
err.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.logon"));
err.add("errinfo", new ActionError("error.logon"));

以上两名有什么区别啊


没什么区别
ActionErrors.GLOBAL_ERROR也是一个字符串。
最好写做ActionErrors.GLOBAL_ERROR
不然的话可能会报错。


posted @ 2006-12-20 18:03 Super·shen BLOG 阅读(2583) | 评论 (3)编辑 收藏
和女主角Action 对象共舞

什么是Action?
和常规的web 应用相比,Struts Action 类工作起来就象一个小型的servlet。在大多数Java
应用中,诸如访问业务层的任务、错误处理等任务均是由servlet 承担的。在一个 Struts 应
用中,servlet 扮演着一个分派器的角色。而Action 对象则干实际的工作。象 servlets 一样,
Action 对象是多线程的。每个应用只需要一个Action 类的实例。

Action做些什么?

一个典型的Action 的职责通常是:

􀂄 ■校验前提条件或者声明
􀂄 ■调用需要的业务逻辑方法
􀂄 ■检测其它处理错误
􀂄 ■将控制路由到相关视图


检验输入: Action 需要做的就是确认ActionForm 是否是需要的类型。

调用逻辑业务:

Action 类是HTTP 与应用系统中其它部分之间的适配器。最重要的是要避免将业务逻辑放入
Action 之中。Action 类应该只是简单地收集业务方法需要的数据并传递它到具体的业务对
象。如果你同时在编写业务类和Action 类,可能会受到要将它们编写在一起的诱惑。一定
要抵挡这种诱惑,并且将业务方法放入Action 可调用的单独的类之中。Java 虚拟机(JVM)
针对这种方法调用作了优化;性能损失可以忽略不计。
同时你也得到了一些设计上的优势

Action检测错误:
Struts具有一个设计良好的错误处理系统,允许你可以:
􀂄■ 同时截获几个错误
􀂄■ 在请求中传递错误数据包
􀂄■ 显示本地化信息

这个处理流程涉及到两个对象 (ActionErrors 和 ActionError) 和一个注册错误的工
具方法(saveErrors) 。其它两个对象 (MessageResources 和 一个定制标签)则用来显
示错误信息

注册错误
总体流程归结为:
􀂄1 创建一个空的ActionErrors 实例
􀂄2 在错误发生时,为错误信息添加关键字;
􀂄3 检查是否添加了某些信息
􀂄4 保存ActionErrors 集合对象到请求中
􀂄5  转发控制到错误页面以显示信息
􀂄6  否则,正常继续

例如
ActionErrors errors = new ActionErrors();
try {
// * 调用业务对象 *
}

catch (ModelException e) {
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("error.detail",e.getMessage()));
}

if (!errors.empty()) {
saveErrors(Request, errors);
return (mapping.findForward("error"));
}

// * 正常继续 *

posted @ 2006-12-14 10:32 Super·shen BLOG 阅读(412) | 评论 (0)编辑 收藏
用Struts开发Web应用

要使用Struts 开发web 应用,开发人员将需要的超链接定义为ActionForward,HTML 表
单定义为ActionForm,定制的服务器端动作定义为Action 类。
需要访问JDBC 和EJB 的开发人员也可通过Action 对象进行他们的工作。这样,表现层不
需要和Model 层打交道。Struts Action 对象将收集View 需要的数据,然后将它们转发到
表现页面。Struts 提供 JSP 标记库,它们将和JSP 页面一起使用,简化 HTML 表单和访
问Action 要转发的其它数据。其它表现机制,比如Velocity templates, 也可用来访问
Struts 框架,以创建动态的web 页面。这种处理流程入下图:{D99B93EF-D27B-42BA-A010-CB465E53C7BD}.BMP



{A1ABCBA2-B893-4911-8786-5CF86A796AFB}.JPG
posted @ 2006-12-06 17:50 Super·shen BLOG 阅读(278) | 评论 (0)编辑 收藏

 

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给我们带来的无穷乐趣。希望以上所述对大家有所帮助。
posted @ 2006-12-06 16:46 Super·shen BLOG 阅读(254) | 评论 (0)编辑 收藏
仅列出标题
共4页: 上一页 1 2 3 4 下一页