解决struts2获取中文参数乱码的问题


1.jsp网页用pageEncoding="UTF-8"

2.编写filter修改request的characterEncoding
package com.test.filter;
import java.io.IOException;
import javax.servlet.*;

public class EncodingFilter implements Filter {

private String encoding;

public void destroy() {
this.encoding = null;

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(this.encoding);
chain.doFilter(request, response);
}

public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
}

}
3.在web.xml设置filter,注意:要设置在struts的filter之前,不然不起作用
   <filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.test.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
   </filter>
   <filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
   </filter-mapping>

你可能感兴趣的:(Web,jsp,xml,struts,servlet)