web.xml中添加
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
com.xinli.webapp.filter.EncodingConvertFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
filter类:
package com.xinli.webapp.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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 此编码过滤器适用于解决struts2.1.6
* 前台jsp页面的中文传入action出现乱码
*
* @author
*
*/
public class EncodingConvertFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse rsp,
FilterChain fc) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest) req;
HttpServletResponse response=(HttpServletResponse) rsp;
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
fc.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}