在使用struts框架的J2EE應用的Web層面解決中文問題有四處關鍵:
1、Web頁面
2、Response
3、Request
4、struts的properties文件
以下是四處的具體說明。
1、Web頁面
在每個jsp頁開頭加上
<%@ page
language="java"
contentType="text/html; charset=GBK"
pageEncoding="GBK"
%>
<META http-equiv="Content-Type" content="text/html; charset=GBK">
其中,GBK也可以替換成utf-8。
2、Response
1的解決辦法中已經(jīng)覆蓋解決了2。
3、Request
最好的解決辦法是寫一個servlet過濾器,代碼如下:
package cn.com.extech.tornado;
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;
/**
* @author
*
* @version
*/
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; /**
/**
* 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;
}
/**
* 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);
}
}
然后在web.xml中增加配置:
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<display-name>SetCharacterEncodingFilter</display-name>
<filter-class>cn.com.extech.tornado.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
此方法只有在form里面用提交的方式將客戶端的輸入提交到后臺才有用,因為,這時候的request的默認編碼方式是iso-8859-1,
如果設置了request.setCharacterEncoding("GB2312"),就是設置newString(***.getByte("ISO-8859-1"),"GB2312");而如果
是用forward方式,再用request.getParameter時,由于用forward提交的param的默認編碼方式不一定ISO-8859-1,所以
request.setCharacterEncoding("GB2312"),可能得不到正確的中文。如何解決forward的param的中文問題呢?
4、struts的properties文件
方法一:
建立ApplicationResources_zh_CN.properties文件并寫入中文,然后在jdk的bin目錄下運行
native2ascii -encoding gb2312 ApplicationResources_zh_CN.properties ApplicationResources_zh_CN_new.properties。
再將ApplicationResources_zh_CN_new.properties改名成ApplicationResources_zh_CN.properties,覆蓋WEB-INF\class目錄
下的同名文件即可。
下載該插件可以非常方便的實現(xiàn)各種編碼間的轉換
插件下載址:http://propedit.sourceforge.jp/eclipse/updates
參考:http://blog.csdn.net/finepeace/archive/2004/06/23/23155.aspx