springmvc 静态资源访问详解编程语言

1、如果要访问WEB-INF文件夹外的静态资源(除html文件以外)直接指定相对路径即可,如果要访问WEB-INF文件夹下的静态资源,需要在dispatch-servlet中配置mapping:

<?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:p="http://www.springframework.org/schema/p"   
    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-3.0.xsd     
  http://www.springframework.org/schema/context     
  http://www.springframework.org/schema/context/spring-context-3.0.xsd   
  http://www.springframework.org/schema/mvc     
  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">   
   
    <context:component-scan base-package="com.busymonkey" />   
    <mvc:resources mapping="/resources/css/**" location="/WEB-INF/resources/css/" /> 
    <mvc:resources mapping="/resources/js/**" location="/WEB-INF/resources/js/" /> 
    <mvc:resources mapping="/resources/imgs/**" location="/WEB-INF/resources/imgs/" /> 
    <mvc:resources mapping="/resources/html/**" location="/WEB-INF/resources/html/" /> 
    <mvc:resources mapping="/resources/fonts/**" location="/WEB-INF/resources/fonts/" /> 
    <bean id="viewResolver"   
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">   
        <property name="viewClass"   
            value="org.springframework.web.servlet.view.JstlView" />  
		<property name="prefix" value="/WEB-INF/jsp/" />   
		<property name="suffix" value=".jsp" />  
    </bean>   
    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->   
    <bean id="multipartResolver" class="com.busymonkey.utils.CustomMultipartResolver">     
         <property name="maxUploadSize" value="3000000000"/>     
         <property name="maxInMemorySize" value="4096"/>     
         <property name="defaultEncoding" value="UTF-8"></property>   
    </bean>  
    <mvc:annotation-driven /> 
    <mvc:default-servlet-handler/> 
</beans>

如果想要在web.xml文件中显示主页为*.html后缀的,需要加入<mvc:default-servlet-handler/>

2、访问WEB-INF文件夹下的静态资源,还需要web.xml文件配置拦截所有请求 “ / ” :

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 
  <display-name>Spring3MVC</display-name> 
  <servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>/</url-pattern> 
  </servlet-mapping> 
  <welcome-file-list> 
    <welcome-file>homePage.html</welcome-file> 
  </welcome-file-list> 
</web-app>

理论上浏览器能直接从url访问这些静态资源,那么html或者jsp文件就能访问,访问的相对路径有两种方式:

"resources/js/jquery-3.1.1.js" 
或者"./resources/js/jquery-3.1.1.js" 
 
"/resources/js/jquery-3.1.1.js"这样是不行的

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16599.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论