我用Filter来解决中文乱码问题,以前也用过,但这次不知道哪里出了问题,一直解决不了乱码问题。大家帮忙看下:xml文件:<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!--Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param> <filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 乱码问题 Filter -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.common.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>enable</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 定义struts2的核心控制器:FilterDispatcher -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
我的Filter:public class SetCharacterEncodingFilter implements Filter {
protected String sEncodingName;
protected FilterConfig sysFilter;
protected boolean bEnable;
// protected Log logger = LogFactory.getLog(SysFilter.class);
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
if (this.bEnable) {
try {
arg0.setCharacterEncoding(this.sEncodingName);
arg1.setContentType("text/html;charset=" + this.sEncodingName);
arg1.setCharacterEncoding(this.sEncodingName);
arg2.doFilter(arg0, arg1);
} catch (Exception e) {
// logger.info("�����");
}
// session.close();
} else {
arg2.doFilter(arg0, arg1);
}
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
this.sysFilter = arg0;
this.loadFilterSetting();
}
private void loadFilterSetting() {
this.sEncodingName = this.sysFilter.getInitParameter("encoding");
// logger.info("encoding:" + sEncodingName);
String sEnable = this.sysFilter.getInitParameter("enable");
if (sEnable != null && sEnable.equalsIgnoreCase("true")) {
this.bEnable = true;
} else {
this.bEnable = false;
}
}
}
另:我的JSP上都用了UTF-8格式,我的项目整个编码也是UTF-8,我的数据库用的是Mysql,utf-8。DEBUG,肯定进了这个Filter,
我在Action里跟踪,上传的中文就已经是乱码了。
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!--Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param> <filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 乱码问题 Filter -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.common.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>enable</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 定义struts2的核心控制器:FilterDispatcher -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
我的Filter:public class SetCharacterEncodingFilter implements Filter {
protected String sEncodingName;
protected FilterConfig sysFilter;
protected boolean bEnable;
// protected Log logger = LogFactory.getLog(SysFilter.class);
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
if (this.bEnable) {
try {
arg0.setCharacterEncoding(this.sEncodingName);
arg1.setContentType("text/html;charset=" + this.sEncodingName);
arg1.setCharacterEncoding(this.sEncodingName);
arg2.doFilter(arg0, arg1);
} catch (Exception e) {
// logger.info("�����");
}
// session.close();
} else {
arg2.doFilter(arg0, arg1);
}
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
this.sysFilter = arg0;
this.loadFilterSetting();
}
private void loadFilterSetting() {
this.sEncodingName = this.sysFilter.getInitParameter("encoding");
// logger.info("encoding:" + sEncodingName);
String sEnable = this.sysFilter.getInitParameter("enable");
if (sEnable != null && sEnable.equalsIgnoreCase("true")) {
this.bEnable = true;
} else {
this.bEnable = false;
}
}
}
另:我的JSP上都用了UTF-8格式,我的项目整个编码也是UTF-8,我的数据库用的是Mysql,utf-8。DEBUG,肯定进了这个Filter,
我在Action里跟踪,上传的中文就已经是乱码了。
解决方案 »
- linux下发布j2ee项目,访问不了,高手指教啊,log如下
- Spring MVC 配置问题,想实现个添加操作,成功后还回到原页面,第一次做,请知道的告诉我下
- JSON联动问题
- 如何用javascript解析xml
- java 调用数据库内容时 无法显示对应数据..请帮忙.谢谢了
- 用ibatis如何判断字段不为空的怎么写
- 题目
- java集成BO 出现的问题
- 关于java导出excel的问题
- 求教,谷歌分析(Google Analytics) Api现在国内能用吗?
- java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parame
- 逐个字符读取txt文件内容遇到麻烦,拱手请教
UTF8一直都是中文处理的最佳编码方式。
URIEncoding解决的是get方式提交的中文编码。你的filter处理不到,可能你页面以url方式提交中文,也可能form的method=get。
所以加上这个就不愁了。看看吧:
web乱码
http://blog.csdn.net/crazylaa/archive/2009/12/24/5066784.aspx
http://seagar-collection.javaeye.com/blog/297149