关于SpringMVC乱码问题

关于SpringMVC运行Tomcat控制台出现乱码的情况(在网上找到一种方法亲测有效)

找到tomcat文件夹中的conf包下的logging.properties中找到

java.util.logging.ConsoleHandler.encoding = UTF-8

将这行代码注销改为

java.util.logging.ConsoleHandler.encoding = GBK

重启tomcat即可!

关于SpringMVC提交表单时,网页出现乱码情况,有两种解决方法

方法一(自定义过滤器):

public class EncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");

        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

在web.xml中配置过滤器

<filter>
        <filter-name>encoding</filter-name>
        <filter-class>com.kuang.filter.EncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

注意点: / 与 /* 的区别

/ : 只匹配所有请求,不会去匹配jsp页面

/* : 匹配所有请求,包括jsp页面


方式二(SpringMVC自带的过滤器)

<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>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 

相关推荐