Spring放行静态资源

在SpringMVC的学习过程中,由于配置文件设定SpringMVC拦截所有请求,导致静态资源如页面、JavaScript、CSS和插件无法正常访问。为了解决这个问题,需要创建一个类`SpringMvcSupport`,该类继承自`WebMvcConfigurationSupport`,重写`addResourceHandlers`方法,明确指定静态资源的路径和位置,从而实现静态资源的正确访问。
摘要由CSDN通过智能技术生成
Springmvc学习中 存在对静态资源访问的时候 在一个配置文件中配置了SprinfMvc拦截所有请求
,使得访问静态资源时也被spring拦截了需要再加一个类继承WebMvcConfigurationSupport 
添加访问过滤。


```package com.itheima.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
    //设置静态资源访问过滤,当前类需要设置为配置类,并被扫描加载
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/cs/**").addResourceLocations("/css/");
        registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");

    }
}

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值