java struts 中文乱码处理
1.写一个过滤器,完整代码如下:
package com.jesun.character.Filter;
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 Jesun
* @date 2008-2-16
* 源码来自Internet,我只作了简单的修改
* 解决Struts中文显示乱码问题
* */
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true; //在web.xml文件里定义filter时要用到此参数
public void destroy() {
// TODO Auto-generated method stub
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// 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);
}
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
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 String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
//class end
(注:假如我们是用tomcat作服务器的话,那就更方便了,我们可以直接在tomcat的webapps\jsp-examples\Web-INF\classes\filters目录中的两个源文件RequestDumperFilter.java,SetCharacterEncodingFilter.java复制到你使用的包下。)
2.写好这个类以后,我们就在WEB应用的web.xml文件里定义Filter类,完整web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- 定义Filter,解决struts中文问题 -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.jesun.character.Filter.SetCharacterEncodingFilter</filter-class>//包名
<init-param>
<!-- 定义编码格式,我用的是GBK -->
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<!-- innore参数是在过滤器类定义的 -->
<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>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>*.jsp</servlet-name>
</filter-mapping>
<!-- Filter 定义结束 -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/jesun.tld</taglib-uri>
<taglib-location>/WEB-INF/jesun.tld</taglib-location>
</taglib>
</web-app>
(3)以后编写JSP文件时,记得加入下面两句:
<%@page contentType="text/html;charset=GBK"%>
<META http-equiv=Content-Type content="text/html;charset=GBK">
到这里我们的工作就做完了,在程序里我们不需要再作任何其它处理,