springboot踩坑一:添加webapp文件夾能訪問jsp卻找不到靜態資源404

     這次項目突發奇想想用一次springboot,但是入坑才發現坑好多啊。

     項目說明:springboot版本是2.2.2,jdk是1.8

     springboot官方推薦使用thymeleaf模板引擎,把靜態資源放到resources下面的static中,然後頁面放到templement中,但這次因爲時間比較緊,所以我想把以前項目裏面的webapp直接搬過來,裏面的jsp直接使用,結果jsp能訪問到,但裏面的圖片,css,js全都報404錯誤。

     目錄結構:

     直接在與java同級創建一個webapp目錄, 然後把以前舊spring MVC項目中的webapp直接複製了過來。

     結果訪問裏面的jsp可以正常訪問到,但靜態資源全都找不到了。

     

單獨訪問css都是都跳轉到報錯頁面

道理我知道點,應該是springboot默認攔截所有uri用作各種處理,然後就把靜態資源也攔了,想不攔就得配置放行。

然後百度了一堆方法不過可能是springboot版本不一樣或者是jdk版本不一樣,反正就是實現不了,prop文件配置也不生效,最後好不容易找到一個我這個版本可以使用的方法:

springboot默認掃描的靜態資源的路徑是這些

classpath:/static

classpath:/public

classpath:/resources

classpath:/META-INF/resources

我們的目標就是就是把webapp中的內容編譯到最後一種classpath:/META-INF/resources中,然後放行這個路徑即可

解決方法:

1.在pom.xml中的build->resources中增加這個,作用是吧webapp編譯到 META-INF/resources 中

<resource>
    <directory>src/main/webapp</directory>
    <targetPath>META-INF/resources</targetPath>
    <includes>
        <include>**/**</include>
    </includes>
</resource>

這種重新編譯後可以看到webapp已經被編譯進去了

2.增加一個配置類

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

@Configuration
public class staticConfig implements WebMvcConfigurer {
    //這個寫不寫沒啥用
   /* @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns("/META-INF/resources*");
    }*/

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/");
    }
}

這樣訪問,我的css,js終於出來了!!! 

如果還有問題,需要檢查是否攔截器中return成false了,如果返回結果不是404,而是500啥的,需要檢查是否是設置了字符集編碼轉換造成的,我找不到的一個原因就是字符集轉換造成的。

ps:雖說springboot放行了靜態資源攔截,我加了登錄攔截器後靜態資源又被攔截器攔截了。

所以又得指定放行相關文件,增加excludePathPatters就行

package com.bomc.enterprise.config;

import com.bomc.enterprise.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    //不註冊這個在過濾器中 service將報空
    @Bean
    public LoginInterceptor loginInterceptor(){
        return new LoginInterceptor();
    }

    //添加攔截器方法
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加攔截路徑
        String[] addPathPatters={
                "/**"
        };
        //添加不攔截路徑
        String[] excludePathPatters={
                "/", "/login/login", "/login/loginPage","/login/register",
                "/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg",
                "/**/*.jpeg", "/**/*.gif", "/**/fonts/*", "/**/*.svg",
                "/**/*.ttf","/**/*.woff","/**/*.eot","/**/*.otf","/**/*.woff2"
        };
        //註冊登錄攔截器
        registry.addInterceptor(loginInterceptor()).addPathPatterns(addPathPatters).excludePathPatterns(excludePathPatters);
        //如果多條攔截器則增加多條
    }

    //添加放行靜態資源
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/");
    }


}

 

 

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