解决SpringBoot中静态资源不能访问的问题
在编写SpringBoot项目上的html页面时,直接调试html页面时,页面可以正常显示。但是在启动项目后,html页面样式丢失。因此想到了,可能是讲台资源被过滤掉了,参考一些大神的博客,终于把这个问题解决了。
解决办法
编写一个Spring配置类,配置静态资源的访问路径即可
项目结构
项目结构如下。在common包下新建StaticPath类,在这个类中继承WebMvcConfigurationSupport并重写addResourceHandlers方法,在方法中配置静态资源的路径即可。最重要的一点是,在类名上加上@Controller和@SpringBootApplication注解。

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Controller
@SpringBootApplication
public class StaticPath extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/templates");
registry.addResourceHandler("/**").addResourceLocations("classpath:/static");
}