MyBatis+SpringMVC+SpringBootSpringFrameworkJava技术精选

SpringMVC实现RESTful接口,关于静态资源的访问

2018-04-11  本文已影响107人  卑微幻想家

前言

web.xml的相关配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>echart-learning</display-name>

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

    <!--加载applicationContext.xml-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--编码过滤器-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--Spring监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--SpringMvc-->
    <servlet>
        <servlet-name>SpringMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>SpringMvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
        

其中需要注意的点是:

<servlet-mapping>
        <servlet-name>SpringMvc</servlet-name>
        <url-pattern>/</url-pattern>  <!-- 这里需要配置成 / -->
</servlet-mapping>

spring-mvc.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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 包扫描 -->
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.zhaojun.controller" />

    <!-- 视图解析器 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/view/" />
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--静态资源映射-->
    <mvc:resources mapping="/resource/**" location="/resource/"/>
</beans>

需要注意的是,由于上面web.xml中,我们springmvc的拦截方式是 / ,这样会导致静态资源也被拦截住,所以需要配置静态资源的映射,这里有五种方法。我使用了第二种,下面是五种方法的相关说明。

  1. 用另外一个servlet来处理静态资源。如果我们的静态资源放在webapp文件夹下的resource文件夹中,那么我们可以用名字为default的servlet来处理静态资源。因此我们需要在web.xml中加上如下配置:

    <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>resource/*</url-pattern>
    </servlet-mapping>
    

    这标识default的servlet会处理URL中为resource/*的请求,这样,当你把image,css,js等文件,放在resource文件夹中,就不会被拦截住了。

  2. 采用Spring自带的 <mvc:resources>方法,在spring-mvc.xml进行以下配置:

    <mvc:annotation-driven/>
    <mvc:resources mapping="/resource/**" location="/resource/"/>
    

    这样,就不需要新添加一个servlet的配置来处理静态资源了。

  3. 在SpringMVC3.0之后推荐使用:

    <mvc:default-servlet-handler/>
    

    在spring-mvc.xml中配置<mvc:default-servlet-handler />后,会在SpringMVC上下文中定义一个org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,它会像一个检查员,对进入DispatcherServlet的URL进行筛查,如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,如果不是静态资源的请求,才由DispatcherServlet继续处理。

    一般Web应用服务器默认的Servlet名称是"default",因此DefaultServletHttpRequestHandler可以找到它。如果你所有的Web应用服务器的默认Servlet名称不是"default",则需要通过default-servlet-name属性显示指定:<mvc:default-servlet-handler default-servlet-name="所使用的Web服务器默认使用的Servlet名称" />

以下两种在SpringMVC3.0之前可以使用。

  1. 在spring-mvc.xml中配置:

    <mvc:resources location="/img/" mapping="/img/**"/>   
     <mvc:resources location="/js/" mapping="/js/**"/>    
     <mvc:resources location="/css/" mapping="/css/**"/> 
    
  2. 在web.xml进行如下配置:

    <servlet-mapping>  
         <servlet-name>default</servlet-name>  
         <url-pattern>*.css</url-pattern>  
    </servlet-mapping>  
      
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.gif</url-pattern>  
      
    </servlet-mapping>  
         
    <servlet-mapping>  
         <servlet-name>default</servlet-name>  
         <url-pattern>*.jpg</url-pattern>  
    </servlet-mapping>  
         
    <servlet-mapping>  
         <servlet-name>default</servlet-name>  
         <url-pattern>*.js</url-pattern>  
    </servlet-mapping>  
    

参考博文: https://blog.csdn.net/suyu_yuan/article/details/52775828

转载请注明出处。

上一篇 下一篇

猜你喜欢

热点阅读