类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:










页面导航:
正文内容:转载
struts中文的解决 发表时间: 2002-9-13 上午9:10
1.使applicationresources.properties支持中文
建立一个applicationresources_iso.properties文件,把应用程序用的message都写进去,然后在dos下执行这个命令,
native2ascii -encoding gb2312 applicationresources_iso.properties applicationresources.properties
这样就会将iso编码的applicationresources转换成gb2312编码的格式了,同时保存到applicationresources.properties.
native2ascii这个工具是jdk自带的一个东东,所以如果path都设定正确就可以直接运行了,你可以在$java_home$/bin下找到他。
转换后的中文类似于这个样子
iso 格式下 :tj.type=商品车类型
gb2312格式下 :tj.type=\u5546\u54c1\u8f66\u7c7b\u578b
然后在struts-config.xml中设置应用这个资源文件
<message-resources parameter="com.huahang.tj.applicationresources" key="org.apache.struts.action.message" />
开发jsp时在jsp的开头写上<%@ page contenttype="text/html; charset=gb2312" %>,将字符集设置成gb2312就可以了。
2.使数据库操作支持中文。
数据库操作支持中文一直让我比较头痛,但是感谢善解人衣向我推荐了www.chinaxp.org,这个网站是用struts框架开发的,而且
开放源码,下载了源码后发现它的中文处理得很好,阅读部分源码,没有发现什么特殊的字符集转换,很纳闷,偶然看到楼上网友
留言知道原来servlet可以统一设置字符转换。chinaxp.org就是这么做的。
在web.xml中加上
<filter>
<filter-name>set character encoding</filter-name>
<filter-class>com.huahang.tj.struts.filters.setcharacterencodingfilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</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>
这里会涉及一个bean,源码如下:
/*
* 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
* users session.</p>
*
* @author <a href="john>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
加上这个后,在action中就可以直接从form中接收gb2312编码的数据了,返回时自然也是gb2312了。
但是这个好像需要servlet 2.2以上的容器
综合上面的方法,我解决了struts中的中文问题,现在还没发现新的问题。