日期:2014-05-19 浏览次数:20690 次
中文乱码过滤器web.xml中
1.
注意乱码过滤器要写在struts2过滤器之前,因为servlet是按照filter的声明先后顺序来执行过滤的
<!-- 中文乱码过滤器 -->
<filter>
<filter-name>encodeFilter</filter-name>
<filter-class>com.kingback.mile.encoding.EncodeFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.
在com.kingback.mile.encoding下建一个EncodeFilter类
package com.kingback.mile.encoding;
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;
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
public class EncodeFilter extends StrutsPrepareAndExecuteFilter implements Filter {
private FilterConfig config = null;
private String encoding = "UTF-8";
@Override
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (encoding == null) {
encoding = config.getInitParameter("encoding");
}
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);<