春季启动-无法加载.css和图像之类的静态文件

时间:2019-05-10 10:58:03

标签: css spring-boot spring-mvc

我不知道为什么Spring Boot无法加载样式表和图像文件。 我的项目结构如下:

enter image description here

负责链接CSS的Thymeleaf代码段:

<link href="../static/css/styles.css" rel="stylesheet" type="text/css">

ViewResolver类配置:

  @Configuration
public class ViewResolverConfiguration implements WebMvcConfigurer {

  @Bean
  public ViewResolver viewResolver(){

    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/");
    resolver.setSuffix(".html");
    return resolver;
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**")
            .addResourceLocations("/resources/");
  }

  @Override
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
  }
}

来自浏览器控制台的消息

enter image description here

0 个答案:

没有答案