【问题标题】:Heroku doesn't see CSS and static txt file in Spring BootHeroku 在 Spring Boot 中看不到 CSS 和静态 txt 文件
【发布时间】:2018-06-01 06:06:18
【问题描述】:

我在 Spring 中创建了一个简单的 Maven Web 应用程序,用于测试密码强度并将其保存到 MySQL 数据库。然后我将其更改为 Spring Boot,以便将其上传到 Heroku。 两个版本都在 localhost:8080 上运行良好,但是当我将 Spring Boot 版本上传到 Heroku 时,它看不到 CSS 文件,也看不到包含单词列表的文本文件,我用来确定密码是否包含任何现有单词英语。 这是我在 Heroku https://passwords2.herokuapp.com 上的应用程序的链接 如您所见,没有背景或字体颜色,并且在尝试查找作为单词列表的“english.txt”文件时会出现错误。 这是 Git Hub 的链接:https://github.com/PiotrDawidziuk/passwords2 唯一不同的是,在 Heroku 版本中,我将数据库选项更改为

spring.datasource.url=${JDBC_DATABASE_URL}
spring.datasource.username=${JDBC_DATABASE_USERNAME}
spring.datasource.password=${JDBC_DATABASE_PASSWORD}
spring.jpa.show-sql=false
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create

在 application.properties 中,我为 PostgreSQL 添加了 Maven 依赖项。 image that shows what files I think Heroku doesn't see

编辑:我使用本教程将我的应用程序部署到 Heroku:https://www.youtube.com/watch?v=7RHYDNSRSOs

【问题讨论】:

  • 指向 Youtube 视频的链接可能对任何人都没有帮助 - 也许您可以在此处报告您从那里获取的相关代码部分?

标签: java spring spring-boot heroku


【解决方案1】:

您的english.txt 将进入一个 JAR,当在 Heroku 中运行时,您的文件将在 JAR 中,因此请尝试将其作为流读取,检查 37233069 以了解如何将文件作为流读取。同样在SpringBoot应用程序中,您不需要WEB-INF中的resources文件夹,在src\main\resources\static中创建css文件夹并将stylesheet.css文件复制到此css文件夹。在 jsps 中,包括 stylesheet.css 如下所示

<link rel="stylesheet" href="/css/stylesheet.css">

【讨论】:

    【解决方案2】:

    您的视图代码中似乎有些混淆,它引用了 static/stylesheet.css,而您的屏幕截图显示了资源文件夹中的 stylesheet.css。 这就是您收到 404(找不到资源)错误的原因。

    可能有一些自定义配置导致它在您的本地环境中工作,但我建议您在发布之前正确调试。

    【讨论】:

      猜你喜欢
      • 2015-10-02
      • 1970-01-01
      • 2022-07-22
      • 1970-01-01
      • 2021-06-03
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      相关资源
      最近更新 更多