springboot thymeleaf靜態資源引入失效問題解決

首先!檢查你的springboot版本是不是2.0x!

衆所周知,springboot2.0以上的版本是基於spring5.0了.
說明什麼問題?告訴你,在底層是有一些變動的,比如攔截器.
這就是爲什麼在springboot1.5能訪問到但是2.0就不行了.

解決辦法:

新建一個config類:

@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
    @Bean
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {

            @Bean
            public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
                WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
                    @Override
                    public void addInterceptors(InterceptorRegistry registry) {
                        //super.addInterceptors(registry);
                        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                                .excludePathPatterns("/bootstrap/**","/image/**","/scss/**","/js/**","/img/**");
                    }
                };
                return  adapter;
            }
        };
        return  adapter;
    }
}

其中:LoginHandlerInterceptor可以做一個HandlerInterceptor的空實現

import org.springframework.web.servlet.HandlerInterceptor;

public class LoginHandlerInterceptor implements HandlerInterceptor {
}

在這裏插入圖片描述

html裏不用管,該怎麼引入還怎麼引入.

在這裏插入圖片描述

# 定義匹配靜態資源路徑
spring.mvc.static-path-pattern=/**
# 定義靜態資源位置
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

順便把以上內容加到你的application.properties裏,雖然可能沒什麼用.

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