1. 程式人生 > >防止spring把靜態資源識別thymeleaf模板

防止spring把靜態資源識別thymeleaf模板

本人在SpringBoot專案中使用thymeleaf模板時,前端靜態資原始檔不能被使用的問題,再此記錄一下

解決:在攔截器中重寫addResourceHandlers方法:

//將templates目錄下的CSS、JS檔案對映為靜態資源,防止Spring把這些資源識別成thymeleaf模版

registry.addResourceHandler("/templates/**.js").addResourceLocations("classpath:/templates/");
registry.addResourceHandler("/templates/**.css").addResourceLocations("classpath:/templates/");
//其他靜態資源
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");