1. 程式人生 > >springMVC訪問靜態資源:為什麼圖片/js/css等檔案寫在jsp中是404不能獲取

springMVC訪問靜態資源:為什麼圖片/js/css等檔案寫在jsp中是404不能獲取

在SpringMVC中常用的就是Controller與View。但是我們常常會需要訪問靜態資源,如html,js,css,image等。

預設的訪問的URL都會被DispatcherServlet所攔截,但是我們希望靜態資源可以直接訪問。該腫麼辦呢?

在配置檔案: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>
複製程式碼

靜態資源訪問,其實方法有多種,如:通過開放tomcat的defaultServlet,修改預設的url-parttern。

但是SpringMVC提供了更為便捷的方式處理靜態資源。

解決方案:

直接在servlet-context.xml中新增資源對映。

我的開發環境:

1、Eclipse Luna SP1

2、Springsource-tool-suite 3.6.4 

修改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目錄作用

WEB-INF是Java的WEB應用的安全目錄。所謂安全就是客戶端無法訪問,只有服務端可以訪問的目錄。 如果想在頁面中直接訪問其中的檔案,必須通過web.xml檔案對要訪問的檔案進行相應對映才能訪問。 當然,你非要放在WEB-INF中,則必須修改resources對映,如:
<resources mapping="/js/**" location="/WEB-INF/js/" />

推薦方式:本文的目錄結構為如下圖所示。