SpringMVC訪問靜態(tài)資源的方法
在SpringMVC中常用的就是Controller與View。但是我們常常會需要訪問靜態(tài)資源,如html,js,css,image等。
默認(rèn)的訪問的URL都會被DispatcherServlet所攔截,但是我們希望靜態(tài)資源可以直接訪問。該腫么辦呢?
在配置文件:web.xml可以看到:
<!-- 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)資源訪問,其實方法有多種,如:通過開放tomcat的defaultServlet,修改默認(rèn)的url-parttern。
但是SpringMVC提供了更為便捷的方式處理靜態(tài)資源。
解決方案:
直接在servlet-context.xml中添加資源映射。
我的開發(fā)環(huán)境:
1、Eclipse Luna SP1
2、Springsource-tool-suite 3.6.4
修改servlet-context.xml,添加resource映射即可。
servlet-context.xml的路徑如下:
配置文件內(nèi)容:
<?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,包括子路徑(即接多個/)
這樣我們就可以直接訪問該文件夾下的靜態(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下面的話,則就會訪問失敗。這個問題常常會犯。
錯誤方式:
WEB-INF目錄作用
WEB-INF是Java的WEB應(yīng)用的安全目錄。所謂安全就是客戶端無法訪問,只有服務(wù)端可以訪問的目錄。
如果想在頁面中直接訪問其中的文件,必須通過web.xml文件對要訪問的文件進(jìn)行相應(yīng)映射才能訪問。
當(dāng)然,你非要放在WEB-INF中,則必須修改resources映射,如:
<resources mapping="/js/**" location="/WEB-INF/js/" />
推薦方式:本文的目錄結(jié)構(gòu)為如下圖所示。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java 字符串內(nèi)存分配的分析與總結(jié)(推薦)
下面小編就為大家?guī)硪黄猨ava 字符串內(nèi)存分配的分析與總結(jié)(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08淺談@FeignClient中name和value屬性的區(qū)別
這篇文章主要介紹了@FeignClient中name和value屬性的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07Java計算一個數(shù)加上100是完全平方數(shù),加上168還是完全平方數(shù)
這篇文章主要介紹了Java計算一個數(shù)加上100是完全平方數(shù),加上168還是完全平方數(shù),需要的朋友可以參考下2017-02-02springboot2中session超時,退到登錄頁面方式
這篇文章主要介紹了springboot2中session超時,退到登錄頁面方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01java并發(fā)編程synchronized底層實現(xiàn)原理
這篇文章主要介紹了java并發(fā)編程synchronized底層實現(xiàn)原理2022-02-02spring注入在有常量的情況下使用@AllArgsConstructor操作
這篇文章主要介紹了spring注入在有常量的情況下使用@AllArgsConstructor操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09