我面临这个问题。我有一个过滤器,它根据过滤器的配置(例如,UTF-8)设置请求的字符编码。这适用于使用 struts html:form 标签编码的表单。但是,如果我使用普通的 HTML 表单标签,数据编码不正确。
这是 web.xml 中的过滤器定义:
<filter>
<filter-name>Encoding Filter</filter-name>
<filter-class>EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这是过滤器:
public class EncodingFilter implements javax.servlet.Filter {
private String encoding;
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
filterChain.doFilter(request, response);
}
public void destroy() {
}
}