【问题标题】:Reload static content spring boot application重新加载静态内容 Spring Boot 应用程序
【发布时间】:2017-04-29 08:17:43
【问题描述】:

我正在使用 Netbeans,并且正在使用 Spring Boot 开发我的第一个 Web 应用程序。我将我的 HTML、js、CSS 保存在“webapp”文件夹中,然后我重构了我的项目并将所有静态内容放在 /resources/static 中。从那时起,我每次都必须重新构建我的项目,因为没有重新加载静态内容。

如果我为 Gulp 使用浏览器同步插件,我可以轻松绕过这个问题吗?

【问题讨论】:

    标签: spring spring-boot static gulp browser-sync


    【解决方案1】:

    将以下内容添加到 src/main/resources/application.properties:

    spring.web.resources.static-locations[0]=file:src/main/resources/static/
    spring.web.resources.static-locations[1]=classpath:/static/
    

    “文件:”会导致刷新浏览器时重新加载内容, see related issue.

    或者,文件resource locations 在运行时可以是discoveredadded programmatically

    另请参阅 documentationtutorial

    请注意,在 Spring Boot 2.4 之前,该属性称为“spring.resources.static-locations”。

    【讨论】:

    • 我还必须从 WebMvcConfigurer 接口的 addResourceHandlers 方法中删除我的 registry.addResourceHandler().addResourceLocations() ,否则我只能在应用程序已经创建目录时才能获取内容。现在它正在工作,感谢这个黑色魔法
    • 只是一个更新 - 现在是 spring.web.resources.static-locations 而不是 spring.resources.static-locations
    【解决方案2】:

    通常静态内容由 spring-boot 插件复制到构建目录(如果您使用 maven,则为 target)。您可以在 {build-directory}/classes/static 中找到您的文件:这些是您应该修改以反映更改的文件。您还应该将更改复制到资源/静态,因为每当您重新启动 spring boot 时,文件都会被复制。

    【讨论】:

    【解决方案3】:

    如果使用application.yml文件进行配置,插入:

    spring:
      web:
        resources:
          static-locations[0]: "file:src/main/resources/static/"
          static-locations[1]: "classpath:/static/"
    

    【讨论】:

    • 注意,在yaml中[n]后缀不是必须的,可以指定'static-locations'作为map键,然后两个字符串可以是列表项(用“-”表示)
    猜你喜欢
    • 2019-07-23
    • 2017-03-01
    • 2017-07-19
    • 2017-04-05
    • 2017-03-25
    • 2019-03-04
    • 2015-05-24
    • 2016-06-24
    • 2022-01-02
    相关资源
    最近更新 更多