spring mvc <mvc:annotation-driven/> 自定義攔截器不走

来源:http://www.cnblogs.com/xiaohuihui96/archive/2016/12/17/6192811.html
-Advertisement-
Play Games

<mvc:annotation-driven/> 這個便簽會註冊2個自定義攔截器,所以導致請求過來就會自己去走註冊的這2個攔截器和定義的一堆bean 但是這個便簽是必須得定義的 直接貼代碼吧 通過自定義攔截器來過濾掉靜態資源 這個問題困擾了我2天,整整熬了2天,翻遍了博客員和百度,各種瞎扯淡,也反思 ...


<mvc:annotation-driven/> 這個便簽會註冊2個自定義攔截器,所以導致請求過來就會自己去走註冊的這2個攔截器和定義的一堆bean

但是這個便簽是必須得定義的

直接貼代碼吧

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="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
        ">
        
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
    
    <context:component-scan base-package="com.muke.controller"  use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan> 
    
    <!-- 對轉向頁面的路徑解析。prefix:首碼, suffix:尾碼 -->
   <!--JSP視圖解析器-->
    <bean id="viewResolverJsp" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/freemarker"/>
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <!-- 解決freemarker中文亂碼 -->
        <property name="contentType" value="text/html;charset=UTF-8"/>        
        <property name="cache" value="true"/>
        <property name="prefix" value=""/>
        <property name="suffix" value=".ftl"/>
        <property name="order" value="0"/>
    </bean>
    
    <!-- 攔截器 -->
    <mvc:interceptors>
        <mvc:interceptor >
            <mvc:mapping path="/**"/>
            <!--對於靜態資源,可以通過尾碼名-->
            <!--<mvc:exclude-mapping path="/**/*.js"/>
            <mvc:exclude-mapping path="/**/*.css"/>
            <mvc:exclude-mapping path="/**/*.jpg"/>
            <mvc:exclude-mapping path="/**/*.gif"/>
            <mvc:exclude-mapping path="/**/*.png"/>-->
            <!--也可以通過文件夾, 加這些exclude-mapping就不會被攔截器攔截到,資源能夠正常訪問-->
            <mvc:exclude-mapping path="/html/**"/>
            <mvc:exclude-mapping path="/back_css/**"/>
            <mvc:exclude-mapping path="/back_js/**"/>
            <mvc:exclude-mapping path="/back_img/**"/>
            <mvc:exclude-mapping path="/back_other/**"/>
            <mvc:exclude-mapping path="/layer/**"/>
            <mvc:exclude-mapping path="/laypage/**"/>
            <bean class="com.muke.springMVC.interceptors.DefaultInterceptors"/>
            
        </mvc:interceptor>
           <mvc:interceptor>
               <mvc:mapping path="/manage/menuBar/**"/>
               <bean class="com.muke.springMVC.interceptors.ManagerPowerInterceptors"></bean>
           </mvc:interceptor> 
        
    </mvc:interceptors>
</beans>

通過自定義攔截器來過濾掉靜態資源

這個問題困擾了我2天,整整熬了2天,翻遍了博客員和百度,各種瞎扯淡,也反思了自己找問題的方式,最後一天自己嘗試著去尋找問題的根源,但還是解決不了

最後只能求救我師傅解決了。只能說自己功力不夠和思考問題的方向錯了吧

還要有這個配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd">

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

</beans>

spring 和spring MVC都會掃描@Controller這個註解,這樣就只掃描一次了

已經忘記這個坑我走了多久了。從配置靜態目錄,少了這個這個標簽導致@Controller這個註解沒生效,到現在,唉真坑


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ...
  • ...
  • 模板方法: 1.layout 2.partial 3.block ...
  • Scala開發環境搭建 先講幾點我學習scala的目的或者原因吧: Scala的開發環境遠比我想的要好搭建的多。下麵就不說太多的廢話了。 一、安裝JDK。 由於Scala本省也是泡在JVM上的一種編程語言,所以要按裝Scala編譯器時,要先安裝JDK,關於JDK的安裝這裡就不在贅述了,我以前也寫過一 ...
  • JavaMail,顧名思義,提供給開發者處理電子郵件相關的編程介面。它是Sun發佈的用來處理email的API。它可以方便地執行一些常用的郵件傳輸。我們可以基於JavaMail開發出類似於Microsoft Outlook的應用程式。 JavaMail包中用於處理電子郵件的核心類是:Session, ...
  • 在寫代碼時發現php foreach引用賦值會導致意外的行為。 代碼示例: <?php $arr = array('a','b','c'); foreach($arr as $k=>&$v) { } print_r($arr); foreach($arr as $k=>$v) { } print_r ...
  • chain 總結:lodash是一個能夠幫助我們更方便的對數組進行各種操作的javascript工具庫 文檔:http://lodashjs.com/docs/#_findindexarray-predicate_identity-thisarg ...
  • ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...