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
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...