<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這個注解沒生效,到現在,唉真坑