欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC訪問靜態(tài)資源的方法

 更新時間:2017年02月04日 14:42:40   作者:停留的風(fēng)  
本篇文章主要介紹了SpringMVC訪問靜態(tài)資源的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

在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)文章

最新評論