近期在做springboot themaleaf專案中遇到首頁css樣式不載入情況,後來發現是註冊攔截器時沒有加入css樣式,下邊是最開始程式碼
public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor( new LoginHandleInterceptor()).addPathPatterns("/**") .excludePathPatterns("/index.html", // 排除掉首页请求 "/", // 排除掉首页请求 ) ; //registry.addInterceptor(new HandlerInterceptor()). }
第一次造訪登入頁面的時候,對應的js css唄攔截器攔截,就沒有加載,只需要把對應的css,jquery等放入到攔截器中就可以了
public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor( new LoginHandleInterceptor()).addPathPatterns("/**") .excludePathPatterns("/index.html", // 排除掉首页请求 "/", // 排除掉首页请求 "/user/login", "/asserts/css/*.css", "/asserts/img/*.svg", "/asserts/js/*.js", "/webjars/bootstrap/4.1.1/css/*.css", "/mancenter/*", "/error", "/asserts/lib/jquery/*","/asserts/lib/*.js") ; //registry.addInterceptor(new HandlerInterceptor()). }
##1.訪問themaleaf頁面報錯
Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Jun 24 11:08:43 CST 2019There was an unexpected error (type=Not Found, status=404).
No message available
錯誤1:
調試時加入了WebMvcConfig類別package com.feilong.Reptile.config; import org.springframework.stereotype.Component; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * 配置静态资源映射 * * @author sunziwen * @version 1.0 * @date 2018-11-16 14:57 **/ @Component public class WebMvcConfig implements WebMvcConfigurer { /** * 添加静态资源文件,外部可以直接访问地址 * * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); } }刪除這個類別後還是報錯,猜測可能是包路徑問題,重新建立個新項目,將舊項目轉移後,沒有再報錯。
以上是springboot themaleaf第一次進頁面不載入css怎麼解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!