【问题标题】:How do I load static resource from another folder using Spring Boot如何使用 Spring Boot 从另一个文件夹加载静态资源
【发布时间】:2019-09-16 19:48:57
【问题描述】:

我有以下结构

ui
  dist
    index.js
backend
  src
    main
      java
        org
          example
            Application.java

我正在尝试将 index.js 呈现为静态资源。我在我的应用程序中尝试了以下...

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/ui/*")
            .addResourceLocations("file:///../ui/dist");
}

但这并没有像我期望的那样托管文件。

如何使用 Spring Boot 将相关文件作为静态资源托管?

【问题讨论】:

    标签: spring-boot gradle spring-web


    【解决方案1】:

    这对我有用...

    String currentPath = new File(".").getAbsolutePath();
    registry.addResourceHandler("/ui/**")
            .addResourceLocations("file:///"+currentPath+"/../ui/dist/");
    

    【讨论】:

      猜你喜欢
      • 2015-03-08
      • 2017-02-25
      • 2017-07-26
      • 2018-12-16
      • 2017-10-13
      • 2017-06-01
      • 2016-08-14
      • 2020-11-09
      • 2017-02-05
      相关资源
      最近更新 更多