spring mvc静态资源访问的配置
生活随笔
收集整理的這篇文章主要介紹了
spring mvc静态资源访问的配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如果我們使用spring mvc來做web訪問請求的控制轉發,那么默認所有訪問都將被DispatcherServlet獨裁統治。比如我現在想訪問的歡迎頁index.html根本無需任何業務邏輯處理,僅僅就展示一句話而已,但spring mvc還是會把訪問index.html這件事交給DispatcherServlet處理,而DispatcherServlet會從HandlerMapping去找對應Controller注解,如果找不到就報錯了:
No mapping found for HTTP request with URI [/index.html] in DispatcherServlet with name 'service-dispatcher'spring mvc提供的解決方案是在配置文件中加入mvc:resources來專門標識靜態資源,比如我的web.xml是這樣的:
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Memcache View Application</display-name><servlet><servlet-name>service-dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>service-dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-core.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list> </web-app>那么我的spring-mvc.xml就要是這樣的:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "><context:component-scan base-package="com.wulinfeng.memcache.view" /><mvc:annotation-driven /><mvc:resources location="/" mapping="/**/*.js"/> <mvc:resources location="/" mapping="/**/*.css"/> <mvc:resources location="/" mapping="/**/*.png"/><mvc:resources location="/" mapping="/*.html"/> </beans>這時就可以順利訪問index.html這個歡迎頁了。其他js、css或png圖片的靜態資源也可以直接訪問。如果我還要做攔截,比如登陸功能,又不想攔截到靜態資源,怎么辦呢?最簡單的就是配置攔截時過濾靜態資源:
<mvc:interceptors><mvc:interceptor><mvc:mapping path="/**" /><mvc:exclude-mapping path="/**/*.css" /><mvc:exclude-mapping path="/**/*.js" /><mvc:exclude-mapping path="/**/*.png" /><mvc:exclude-mapping path="*.html" /><mvc:exclude-mapping path="/login**" /><mvc:exclude-mapping path="/register**" /><mvc:exclude-mapping path="/getVerifyCode**" /><mvc:exclude-mapping path="/getMethod/**" /><bean class="com.wulinfeng.test.testpilling.util.InterceptorUtil" /></mvc:interceptor></mvc:interceptors>?
轉載于:https://www.cnblogs.com/wuxun1997/p/7884733.html
總結
以上是生活随笔為你收集整理的spring mvc静态资源访问的配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TestCenter中测试需求、测试用例
- 下一篇: 阿里云ecs禁止ping,禁止telne