Spring Mvc攔截器不起作用

https://blog.csdn.net/chtjava/article/details/80963877?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

https://blog.csdn.net/An_xiaowu/article/details/84315754?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

今天在Spring項目中使用到了攔截器,但是配置完成之後死活不起作用。

網上有的說是因爲在spring-mvc.xml文件中配置了 

<mvc:annotation-driven/>此標籤,導致攔截器不起作用,因爲在這個標籤中默認有一個攔截器《
這句會其實它已經註冊了一個DefaultAnnotationHandlerMapping ,而後面你自己註冊的優先級沒它內部的高,所以一直都不會調用你註冊的攔截器。
》於是我按照此方法試了一下,把這個標籤去掉,但是很遺憾報錯了。
報錯說:no mapping,大概就是說找不到映射的路徑。

************************************************************************************************

既然

<mvc:annotation-driven/>此標籤中有默認的攔截器,那麼我就在
<!--攔截器-->
<mvc:interceptors>
    <bean class="com.pdd.interceptor.PddInterceptor"/>  在這裏把攔截器的全類名聲明好,結果問題解決了
    <mvc:interceptor>
        <!--暫時默認攔截所有請求,到時根據需求改變-->
        <mvc:mapping path="/*"/>
        <bean class="com.pdd.interceptor.PddInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

 

今天項目中需要加springMVC攔截器,但是加上了不起作用,我仔細查看了一下,原來是因爲在springmvc.xml文件中配置的這個<mvc:annotation-driven/>   配置文件導致的攔截器失效,  我把這個註釋掉,改成原始的包掃描的方式獲取controller,就起作用了.改成如下這種

<context:component-scan base-package="com.baidu.controller" use-default-filters="false">
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
 

https://blog.csdn.net/z3881006/article/details/78253935

使用mvc:annotation-driven的時候,使用不了DefaultAnnotationHandlerMapping的問題,解決辦法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章