[05] SpringMVC-- static resource filter

[05] SpringMVC notes - Static Resources filter

Add filter configuration file springMVC

<!-- 前端控制器,配置哪些静态资源不拦截-->
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>

Note: springMVC interceptor, if configured to intercept the path is "/ **", or static resources will be blocked


Solution:

  1. Use the default Servlet static resource processor
  2. Modify global Spring interceptor to intercept * .do
  3. A single static resource configuration in which the interceptor SpringMVC Do not intercept (not recommended

The first:

  1. Arranged in spring-mvc.xml
<!--使用默认的静态资源 处理Servlet处理静态资源 -->
<mvc:default-servlet-handler/>
  1. Then configure (preamplifier front controller in web.xml
 <!--解除SpringMVC拦截器对静态资源文件的拦截-->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
        <url-pattern>*.css</url-pattern>
        <url-pattern>/assets/*"</url-pattern>
        <url-pattern>/img/*</url-pattern>
        <url-pattern>/fonts/*</url-pattern>
    </servlet-mapping>


The second:

  1. web.xml Configuration
<servlet>
    <servlet-name>dispatcherServlet</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>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>


Third:

  1. spring-mvc.xml arrangement
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**/*"/> 
        <mvc:exclude-mapping path="/**/*.css"/>
        <mvc:exclude-mapping path="/**/*.js"/>
        <mvc:exclude-mapping path="/**/*.png"/>
        <mvc:exclude-mapping path="/**/*.gif"/>
        <mvc:exclude-mapping path="/**/*.jpg"/>
        <mvc:exclude-mapping path="/**/*.jpeg"/>
        <mvc:exclude-mapping path="/**/fonts/*"/>
        
        <bean class="com.luwei.console.mg.interceptor.MyInterceptor"></bean>
    </mvc:interceptor>
</mvc:interceptors>
Published 58 original articles · won praise 15 · views 8544

Guess you like

Origin blog.csdn.net/qq_42701294/article/details/104790210