spring boot使用遇到static资源无法加载问题

pom.xml中集成了thymealeaf

<!-- Themeleaf -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

application.yml对静态资源进行配置

spring:
  profiles:
    active: dev

#  视图层
  mvc:
    view:
      prefix: classpath:/templates/
      suffix: .html
    static-path-pattern: /**
  resources:
    static-locations: classpath:/static/
  #  热部署生效
  devtools:
    restart:
      enabled: true
      additional-paths: src/main/java
  thymeleaf:
    mode: HTML5
    prefix: classpath:/templates/

然后还是遇到了万恶的404 not found, css/js/image等资源都无法加载进去,控制台也没有报错,反复确人路径有没有写错,emmm不是这些问题

最后的解决办法:自定义资源路径,并重新指向static文件夹

package com.pplibaby.blog.Config;

        import org.springframework.context.annotation.Configuration;
        import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
        import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @ClassName ViewConfig
 * @Dsecription TODO
 * @Author ppilbaby
 * @Date 2019/6/18 19:08
 **/

@Configuration
public class ViewConfig extends WebMvcConfigurerAdapter {

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

虽然网上说默认是static但是还是报错,很无奈,就用了这个折中办法来解决问题。。。。

发布了24 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Wutongyewan/article/details/92800703