在JSP中常见问题,防止SpringMVC拦截器拦截js等静态资源文件的解决方案

方案一、拦截器中增加针对静态资源不进行过滤(涉及spring-mvc.xml)

<mvc:resources location="/" mapping="/**/*.js"/>
<mvc:resources location="/" mapping="/**/*.css"/>
<mvc:resources location="/assets/" mapping="/assets/**/*"/>
<mvc:resources location="/images/" mapping="/images/*" cache-period="360000"/>

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**/*"/>
        <mvc:exclude-mapping path="/**/fonts/*"/>
        <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="/**/*login*"/>
        <mvc:exclude-mapping path="/**/*Login*"/>
        <bean class="com.luwei.console.mg.interceptor.VisitInterceptor"></bean>
    </mvc:interceptor>
</mvc:interceptors>

  

方案二、使用默认的静态资源处理Servlet处理静态资源(涉及spring-mvc.xml, web.xml)

在spring-mvc.xml中启用默认Servlet

1 <mvc:default-servlet-handler/>

  在web.xml中增加对静态资源的处理

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.css</url-pattern>
    <url-pattern>/assets/*"</url-pattern>
    <url-pattern>/images/*</url-pattern>
</servlet-mapping>

  * 但是当前的设置必须在Spring的Dispatcher的前面

方案三、修改Spring的全局拦截设置为*.do的拦截(涉及web.xml)

<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>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

  

这样设置,Spring就会只针对以‘.do‘结尾的请求进行处理,不再维护静态资源

针对这三种方案的优劣分析:

  第一种方案配置比较臃肿,多个拦截器时增加文件行数,不推荐使用;

  第二种方案使用默认的Servlet进行资源文件的访问,Spring拦截所有请求,然后再将资源文件交由默认的Sevlet进行处理,性能上少有损耗;

  第三种方案Spring只是处理以‘.action‘结尾的访问,性能上更加高效,但是再访问路径上必须都以‘.action‘结尾,URL不太文雅;

综上所述,推荐使用第二和第三种方案

在SpringMVC3.0之后推荐使用: 

<mvc:resources location="/WEB-INF/html/" mapping="/**/*.html"/>
<mvc:resources location="/WEB-INF/html/" mapping="/**/*.ico"/>
<mvc:resources location="/WEB-INF/html/" mapping="/**/*.js"/>
<mvc:resources location="/WEB-INF/html/" mapping="/**/*.css"/>
<mvc:resources location="/WEB-INF/html/" mapping="/**/*.png"/>
<mvc:resources location="/WEB-INF/html/" mapping="/**/*.gif"/>
<mvc:resources location="/WEB-INF/html/" mapping="/**/*.jpg"/>
<mvc:resources location="/WEB-INF/html/" mapping="/**/*.ttf"/>
<mvc:resources location="/WEB-INF/html/" mapping="/**/*.woff"/>
<mvc:resources location="/WEB-INF/html/" mapping="/**/*.woff2"/>

  

原文地址:https://www.cnblogs.com/charlypage/p/9061930.html

时间: 2024-08-25 02:31:19

在JSP中常见问题,防止SpringMVC拦截器拦截js等静态资源文件的解决方案的相关文章

spring 拦截器拦截点的配置

实用正则org.springframework.aop.support.RegexpMethodPointcutAdvisor 然后 <property name="advice"> <ref local="methodCacheInterceptor"/> </property> <property name="patterns"> <list> <value>.*_cac

关于Spring boot2.0+配置拦截器拦截静态资源的问题

第一次遇到这个问题的时候,简直是一脸蒙逼,写了一个拦截器以后,静态资源就不能访问了,到处查找才知道是版本问题 解决办法: 第一步:定义一个类实现 实现WebMvcConfigurer的类中拦截器中添加放行资源处添加静态资源文件路径: @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(sessionInterceptor).addPathPatterns("/&

springboot2.0 使用拦截器后,导致静态文件访问不到的解决方案

1 package com.leenleda.ward.tv.admin.interceptor; 2 3 import com.leenleda.ward.tv.common.config.LeenledaConfig; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.context.annotation.Configuration; 6 import o

Spring MVC程序中怎么得到静态资源文件css,js,图片文件的路径问题

问题描述 在用springmvc开发应用程序的时候.对于像我一样的初学者,而且还是自学的人,有一个很头疼的问题.那就是数据都已经查出来了,但是页面的样式仍然十分简陋,加载不了css.js,图片等资源文件,如果你输入某个css的文件在浏览器中得到的直接是404错误(例如http://localhost:8080/iMax_view/css/classify.css),表示路径肯定没有错,这是因为你在web.xml中配置的springservlet ]<!-- springmvc前端控制器,rest

SpringMVC对静态资源文件的访问(配置)

<!-- 自动扫描的包名 -->     <context:component-scan base-package="com.app,com.core,JUnit4" ></context:component-scan>          <!-- 默认的注解映射的支持 -->     <mvc:annotation-driven />          <!-- 视图解释类 -->     <bean cl

springmvc基础篇—处理图片静态资源文件

当我们在web.xml中对DispatcherServlet的过滤设置为/ 的时候,表示对所有的路径进行拦截过滤,那么不可避免的就会产生一个问题,那就是像图片这种静态资源文件我明明引用路径有,但就是加载不出来图片的情况,针对这种情况,下面做一些处理: 找到spring-mvc.xml配置文件 在里面写加上这行代码即可. <!-- 处理图片静态资源 --> <mvc:resources location="/images/" mapping="/images/

Spring MVC程序中得到静态资源文件css,js,图片文件的路径问题总结

上一篇 | 下一篇 Spring MVC程序中得到静态资源文件css,js,图片 文件的路径 用 Spring MVC 开发应用程序,对于初学者有一个很头疼的问题,那就是程序数据都已经查询出来了,但界面样式仍然十分丑陋,加载不了 css,js,图片等资源文件.当你在浏览器上直接输入某个css文件的路径时,直接得到404错误,而路径肯定没有错,其原因就在于在web.xml 中配置了类似如下的 spring servlet: 程序代码 <servlet>    <servlet-name&g

Spring MVC程序中得到静态资源文件css,js,图片

转载自:http://www.blogjava.net/fiele/archive/2014/08/24/417283.html 用 Spring MVC 开发应用程序,对于初学者有一个很头疼的问题,那就是程序数据都已经查询出来了,但界面样式仍然十分丑陋,加载不了 css,js,图片等资源文件.当你在浏览器上直接输入某个css文件的路径时,直接得到404错误,而路径肯定没有错,其原因就在于在web.xml 中配置了类似如下的 spring servlet: 程序代码 <servlet>    

仿淘宝 css,js 等静态资源合并压缩输出的 jsp 脚本 combo.jsp

原文:仿淘宝 css,js 等静态资源合并压缩输出的 jsp 脚本 combo.jsp 源代码下载地址:http://www.zuidaima.com/share/1550463482612736.htm 仿淘宝 css,js 等静态资源合并压缩输出的 jsp 脚本 自己在项目中有用到,用于脚本合并输出 , 使用示例: <link rel="stylesheet" type="text/css" href="http://www.zuidaima.c