SpringMVC乱码过滤器配置

时间:2022-12-11 07:57:30
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

上诉方式只对post请求有效

如需要对get 全局请求有效,一种方式修改Tomcat server.xml文件

<Connector port="8080" maxHttpHeaderSize="8192" 
              maxThreads="150" minSpareThreads="25" maxSpareThreads="75" 
              enableLookups="false" redirectPort="8443" acceptCount="100" 
              connectionTimeout="20000" disableUploadTimeout="true" uRIEncoding="UTF-8"/>

另一种自定义利用包装类来过滤 或者动态代理

贴一下Warper方式

/**
 * 
 * 版权所有 All Rights Reserved 需改成动态代理(AOP)方式
 * 
 * @author Irving 
 * @date 2013年2月15日2:44:23
 * @version 1.0.0
 * @email zhouyongtao@outlook.com
 */
public class CharacterEncodingFilter implements Filter {

    private FilterConfig config;
    private String defaultCharset = "UTF-8";
    private static Logger logger = LogManager.getLogger("CharacterEncodingFilter");

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        String charset = this.config.getInitParameter("encoding");
        if (charset == null) {
            charset = defaultCharset;
        }

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        request.setCharacterEncoding(charset);
        response.setCharacterEncoding(charset);
        response.setContentType("text/html;charset=" + charset);
        chain.doFilter(new MyRequest(request), response);
    }

    class MyRequest extends HttpServletRequestWrapper {

        private HttpServletRequest request;

        public MyRequest(HttpServletRequest request) {
            super(request);
            this.request = request;
        }

        @Override
        public String getParameter(String name) {

            String value = this.request.getParameter(name);
            if (!request.getMethod().equalsIgnoreCase("get")) {
                return value;
            }

            if (value == null) {
                return null;
            }

            try {
                return value = new String(value.getBytes("iso8859-1"), request.getCharacterEncoding());
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }

        }
    }

    public void destroy() {

    }

    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("========== 启用解决全站乱码Filter =======");
        this.config = filterConfig;
    }