今天创建一个新的Spring Boot项目,没注意到spring boot的版本,发现静态资源无法访问。百度一下发现好像是Spring Boot 2.0版本以后static目录不能直接访问。
接下来直接上解决方法。

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 静态资源映射
 */
@Component
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }
}

相关文章:

  • 2022-12-23
  • 2021-12-11
  • 2022-01-12
  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-19
猜你喜欢
  • 2023-01-12
  • 2022-01-14
  • 2021-05-03
  • 2021-05-31
  • 2022-12-23
  • 2021-10-19
相关资源
相似解决方案