设置interceptor后,get请求乱码

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小尤 2021-09-23 16:17:02

同学你好,1、同学尝试在applicationContext.xml中配置消息转换器试一下。如下所示:

https://img1.sycdn.imooc.com//climg/614c2e7f0950e81009310455.jpg

<!--启用Spring MVC的注解开发模式-->
<mvc:annotation-driven >
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/plain;charset=utf-8</value>
                    <value>text/html;charset=utf-8</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
<!-- 将图片/JS/CSS等静态资源排除在外,可提高执行效率 -->
<mvc:default-servlet-handler/>

2、同学也可以尝试在web.xml中设置字符过滤器。如下所示:

https://img1.sycdn.imooc.com//climg/614c303e0937667608630497.jpg

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

3、最后关闭Tomcat,重构项目试一下。如下所示:

https://img1.sycdn.imooc.com//climg/614c3064091317c602320132.jpg

祝学习愉快!

  • 提问者 慕设计7678942 #1

    以上的我都添加了,也build project还是乱码

    2021-09-23 17:35:46
  • 提问者 慕设计7678942 #2

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <!--通过拦截所有的请求,执行对应的Controller方法-->
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <filter>
            <!--是对post的请求体中所有的字符集转换成UTF-8-->
            <filter-name>characterFilter</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>characterFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <filter>
            <filter-name>formContentFilter</filter-name>
            <filter-class>org.springframework.web.filter.FormContentFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>formContentFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <context:component-scan base-package="com.imooc.restful"/>
        <mvc:annotation-driven>
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <!--响应输出到浏览器后,让浏览器按照text/html;charset=utf-8的格式解决html中乱码问题-->
                            <value>text/html;charset=utf-8</value>
                            <value>application/json;charset=utf-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
        <!--将其他静态资源排除在外-->
        <mvc:default-servlet-handler/>
    
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/**"/>
                <bean class="com.imooc.restful.interceptor.MyInterceptor"/>
            </mvc:interceptor>
        </mvc:interceptors>
    
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/**"/>
                <mvc:exclude-mapping path="/resources/**"/>
                <bean class="com.imooc.restful.interceptor.AccessHistoryInterceptor"/>
            </mvc:interceptor>
        </mvc:interceptors>
    </beans>


    2021-09-23 17:37:33
  • 好帮手慕小尤 回复 提问者 慕设计7678942 #3

    同学你好,1、同学可以使用课程源码试一下,查看是否存在该问题,如果存在,则可能是同学环境有关系,建议同学修改浏览器的字符集与项目字符集试一下。

    2、未复现同学问题,同学可以以复制粘贴的方式反馈相关代码,便于老师测试代码与定位问题。

    祝学习愉快!

    2021-09-23 18:30:26
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师