SpringMVC请求乱码问题

  今天做一个项目实现插入数据的功能,最开始没有添加FilterEncoding处理字符集乱码了,那是正常的,后来我添加过之后依然还是乱码,让我

百思不得其解,代码配置如下:

  

EncodingFilter .java
public class EncodingFilter implements Filter {

    private String encode = null;
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        encode = null;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        if(null == request.getCharacterEncoding()){
            request.setCharacterEncoding(encode);
        }
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

        String encode = filterConfig.getInitParameter("encoding");
        if(this.encode == null){
            this.encode = encode;
        }
    }


}

 

 web.xml 配置

 <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>com.smy.util.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>

  我反复检查了好几遍,包括(项目编码,jsp页面编码。。。),没发现问题啊,最后无奈又把 Tomcat 编码检查了一下,发现 Tomcat 少了字符集设置,

Tomcat默认字符集是 ISO-8859-1 ,于是添加这一句:

  

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8" />

  就这样,乱码问题就解决了。。

posted @ 2017-08-02 11:18  孤独是1态度  阅读(339)  评论(0编辑  收藏  举报