SpringMVC訪問靜態資源


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

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

在配置文件:web.xml可以看到:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>Archetype Created Web Application</display-name>

    <!--welcome pages-->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!--配置springmvc DispatcherServlet-->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <!--配置dispatcher.xml作為mvc的配置文件-->
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--把applicationContext.xml加入到配置文件中-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

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

 

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

解決方案:

直接在dispatcher-servlet.xml中添加資源映射。

我的開發環境

IDEA

修改dispatcher-servlet.xml,添加resource映射即可。

dispatcher-servlet.xml的路徑如下:

配置文件內容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--此文件負責整個mvc中的配置-->

    <!--啟用spring的一些annotation -->
    <context:annotation-config/>

    <!-- 配置注解驅動 可以將request參數綁定到controller參數上 -->
    <mvc:annotation-driven/>

    <!--靜態資源映射-->
    <!--本項目把靜態資源放在了webapp的statics目錄下,資源映射如下-->
    <mvc:resources mapping="/css/**" location="/statics/css/"/>
    <mvc:resources mapping="/js/**" location="/statics/js/"/>
    <mvc:resources mapping="/image/**" location="/statics/image/"/>

    <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前后綴(如果最后一個還是表示文件夾,則最后的斜杠不要漏了) 使用JSP-->
    <!-- 默認的視圖解析器 在上邊的解析錯誤時使用 (默認使用html)- -->
    <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/view/"/><!--設置JSP文件的目錄位置-->
        <property name="suffix" value=".jsp"/>
        <property name="exposeContextBeansAsAttributes" value="true"/>
    </bean>

    <!-- 自動掃描裝配 -->
    <context:component-scan base-package="example.controller"/>
</beans>

資源映射

<!--靜態資源映射-->
    <!--本項目把靜態資源放在了webapp的statics目錄下,資源映射如下-->
    <mvc:resources mapping="/css/**" location="/statics/css/"/>
    <mvc:resources mapping="/js/**" location="/statics/js/"/>
    <mvc:resources mapping="/image/**" location="/statics/image/"/>

mapping:映射

location:本地資源路徑,注意必須是webapp根目錄下的路徑。

兩個*,它表示映射resources/下所有的URL,包括子路徑(即接多個/)

這樣我們就可以直接訪問該文件夾下的靜態內容了。

如:

http://localhost:8080/springMVC1/css/test.css

效果:

陷阱:

配置的location一定要是webapp根目錄下才行,如果你將資源目錄,放置到webapp/WEB-INF下面的話,則就會訪問失敗。這個問題常常會犯。

錯誤方式:

WEB-INF目錄作用

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

WEB-INF目錄作用

WEB-INF是Java的WEB應用的安全目錄。所謂安全就是客戶端無法訪問,只有服務端可以訪問的目錄。
如果想在頁面中直接訪問其中的文件,必須通過web.xml文件對要訪問的文件進行相應映射才能訪問。
 
當然,你非要放在WEB-INF中,則必須修改resources映射,如:
<resources mapping="/js/**" location="/WEB-INF/js/" />
推薦方式:本文的目錄結構為如下圖所示。
轉自:https://www.cnblogs.com/yank/p/4477204.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM