SpringBoot——靜態資源被攔截的問題解決方法

前幾天遇到一個問題,百思不得其解:
當controller跳轉到頁面的時候,會自動給靜態資源加上控制器的@RequestMapping前綴,導致靜態資源最後報404找不到的錯誤。

解決辦法:

在類中寫一個攔截器,讓過濾器能忽略靜態資源!

package com.wjh.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    @Override
    protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章