在SpringMVC中常用的就是Controller與View。但是我們常常會(huì)需要訪問(wèn)靜態(tài)資源,如html,js,css,image等。
默認(rèn)的訪問(wèn)的URL都會(huì)被DispatcherServlet所攔截,但是我們希望靜態(tài)資源可以直接訪問(wèn)。該腫么辦呢?
在配置文件:web.xml可以看到:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- 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 > |
靜態(tài)資源訪問(wèn),其實(shí)方法有多種,如:通過(guò)開(kāi)放tomcat的defaultServlet,修改默認(rèn)的url-parttern。
但是SpringMVC提供了更為便捷的方式處理靜態(tài)資源。
解決方案:
直接在servlet-context.xml中添加資源映射。
我的開(kāi)發(fā)環(huán)境:
1、Eclipse Luna SP1
2、Springsource-tool-suite 3.6.4
修改servlet-context.xml,添加resource映射即可。
servlet-context.xml的路徑如下:
配置文件內(nèi)容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<? 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 > |
資源映射
1
2
3
4
|
<!-- 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根目錄下的路徑。
兩個(gè)*,它表示映射resources/下所有的URL,包括子路徑(即接多個(gè)/)
這樣我們就可以直接訪問(wèn)該文件夾下的靜態(tài)內(nèi)容了。
如:
http://localhost:8090/firstapp/images/cookie.png
http://localhost:8090/firstapp/js/jquery-1.11.2.js
效果:
陷阱:
配置的location一定要是webapp根目錄下才行,如果你將資源目錄,放置到webapp/WEB-INF下面的話,則就會(huì)訪問(wèn)失敗。這個(gè)問(wèn)題常常會(huì)犯。
錯(cuò)誤方式:
WEB-INF目錄作用
WEB-INF是Java的WEB應(yīng)用的安全目錄。所謂安全就是客戶端無(wú)法訪問(wèn),只有服務(wù)端可以訪問(wèn)的目錄。
如果想在頁(yè)面中直接訪問(wèn)其中的文件,必須通過(guò)web.xml文件對(duì)要訪問(wèn)的文件進(jìn)行相應(yīng)映射才能訪問(wèn)。
當(dāng)然,你非要放在WEB-INF中,則必須修改resources映射,如:
1
|
< resources mapping = "/js/**" location = "/WEB-INF/js/" /> |
推薦方式:本文的目錄結(jié)構(gòu)為如下圖所示。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/yank/p/4477204.html