在SpringMVC中經常使用的就是Controller與View。可是咱們經常會須要訪問靜態資源,如html,js,css,image等。css
默認的訪問的URL都會被DispatcherServlet所攔截,可是咱們但願靜態資源能夠直接訪問。該腫麼辦呢?html
在配置文件:web.xml能夠看到:jquery
<!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
靜態資源訪問,其實方法有多種,如:經過開放tomcat的defaultServlet,修改默認的url-parttern。web
可是SpringMVC提供了更爲便捷的方式處理靜態資源。spring
解決方案:spring-mvc
直接在servlet-context.xml中添加資源映射。tomcat
個人開發環境:安全
一、Eclipse Luna SP1cookie
二、Springsource-tool-suite 3.6.4 mvc
修改servlet-context.xml,添加resource映射便可。
servlet-context.xml的路徑以下:
配置文件內容:
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <resources mapping="/images/**" location="/images/" /> <resources mapping="/js/**" location="/js/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="com.yank.firstapp" /> </beans:beans>
資源映射
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <resources mapping="/images/**" location="/images/" /> <resources mapping="/js/**" location="/js/" />
mapping:映射
location:本地資源路徑,注意必須是webapp根目錄下的路徑。
兩個*,它表示映射resources/下全部的URL,包括子路徑(即接多個/)
這樣咱們就能夠直接訪問該文件夾下的靜態內容了。
如:
http://localhost:8090/firstapp/images/cookie.png
http://localhost:8090/firstapp/js/jquery-1.11.2.js
效果:
配置的location必定要是webapp根目錄下才行,若是你將資源目錄,放置到webapp/WEB-INF下面的話,則就會訪問失敗。這個問題經常會犯。
錯誤方式:
WEB-INF目錄做用
<resources mapping="/js/**" location="/WEB-INF/js/" />
推薦方式:本文的目錄結構爲以下圖所示。