解決方法:web
1.手動開發一個Myfilter實現filter接口,代碼以下app
public class MyFilter implements Filter { //讀取局部初始化參數 private String encoding; @Override public void init(FilterConfig filterConfig) throws ServletException { encoding = filterConfig.getInitParameter("encoding"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); chain.doFilter(request,response); //放行 } @Override public void destroy() { } }
2.配置web.xmlide
<!--解決post方式的中文亂碼--><filter> <filter-name>charset</filter-name> <filter-class>com.rjxy.controller.MyFilter</filter-class> <!--設置局部初始化參數--> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param></filter><filter-mapping> <filter-name>charset</filter-name> <url-pattern>/*</url-pattern></filter-mapping>