posts - 42, comments - 7, trackbacks - 0, articles - 1

struts框架中文问题解决(转)

Posted on 2007-10-11 15:31 TuringJava 阅读(306) 评论(0)  编辑  收藏 所属分类: Structs

在使用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的解决办法中已经覆盖解决了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目录
下的同名文件即可。

方法二:

下载该插件可以非常方便的实现各种编码间的转换

插件下载址:http://propedit.sourceforge.jp/eclipse/updates

参考:http://blog.csdn.net/finepeace/archive/2004/06/23/23155.aspx


只有注册用户登录后才能发表评论。


网站导航: