日期:2014-05-17 浏览次数:20906 次
<filter> <filter-name>struts</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>所以应该在该Filter里配置编码.查看 org.apache.struts2.dispatcher.FilterDispatcher 的init方法中没有encoding参数.所以就自己创建个Filter,如下
import java.io.IOException; 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.FilterDispatcher; public class NewFilterDispatcher extends FilterDispatcher { private static String encoding = "UTF-8"; public void init(FilterConfig filterConfig) throws ServletException { super.init(filterConfig); String encodingParam = filterConfig.getInitParameter("encoding"); //获取web.xml下的初始化参数 if (encodingParam != null && encodingParam.trim().length() != 0) { encoding = encodingParam; } } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(encoding); //设置request编码格式 response.setCharacterEncoding(encoding);//设置response编码格式 super.doFilter(request, response, chain); } }再在web.xml中进行配置
<filter> <filter-name>struts</filter-name> <filter-class> com.filter.NewFilterDispatcher </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>问题解决.