【问题标题】:Spring Boot app deployed to Heroku doesn't locate its' thymeleaf views部署到 Heroku 的 Spring Boot 应用程序找不到它的 thymeleaf 视图
【发布时间】:2017-11-02 15:53:12
【问题描述】:

我的 Spring Boot 应用程序在 localhost 上完美运行,但是当我将它部署到 Heroku 时,我的应用程序控制器停止查看通常位于 /templates/ 目录的视图。为什么会这样?我如何确定 heroku 确实上传并编译了我的视图?如果是这样,我是否应该更改 @Controller 类的 @RequestMapping 的实际值,以使它们在 heroku 时可以访问?

你可以在这里找到我的整个工作网络应用程序:https://github.com/slavicketernity/testik56

这是我上传并运行的应用程序:https://testik56app.herokuapp.com/login

【问题讨论】:

    标签: java spring heroku spring-boot thymeleaf


    【解决方案1】:

    在我的情况下,控制器方法返回的带有模板位置的字符串开头的斜杠是错误的。

    在我更改了控制器方法返回的字符串之后

    @RequestMapping(value = "/orders/{orderId}/create_entry")
    String create(@PathVariable String orderId) {
        return "/order_entries/create";
    }
    

    @RequestMapping(value = "/orders/{orderId}/create_entry")
    String create(@PathVariable String orderId) {
        return "order_entries/create";
    }
    

    然后它开始工作了。

    【讨论】:

    • 这是个好主意。我不确定这是不是我的情况,但是,这个斜线主题确实是最容易出错的主题之一。初始斜线指向项目类路径的根目录,而没有初始斜线使路径相对。
    【解决方案2】:

    您是否将此应用程序部署为 .jar 文件?我看到一些基础架构要求您将应用程序部署为 .war 以提供对网页目录的访问。

    如果这是问题,您可以通过 gradle 应用 war 插件。 https://docs.gradle.org/current/userguide/war_plugin.html

    【讨论】:

    • 我将它部署为一个带有嵌入式 tomcat 容器的应用程序。这意味着,它既不是 .jar 也不是 .war。部署它时,我使用了这些说明:link
    • 对。我熟悉嵌入式 tomcat 容器。你能把日志打印出来吗?
    • 请查看我在重新启动其“dyno”后初始化应用程序的完整日志以及我第一次尝试运行它(访问应用程序的链接)here
    • 当然,这里是:2017-06-01T16:46:16.206102+00:00 app[web.1]: 2017-06-01 16:46:16.202 ERROR 4 --- [io-40292-exec-3] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-40292-exec-3] Exception processing template "/login": Error resolving template "/login", template might not exi st or might not be accessible by any of the configured Template Resolvers 2017-06-01T16:46:16.206133+00:00 app[web.1]: org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/login", template might not exist or might not be accessible by any of the configured Template Resolvers
    • 哦。您可能必须将模板文件放在 webapp 目录中。 src/main/webapp/ 这应该可以解决问题。
    猜你喜欢
    • 2021-07-25
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 2020-05-08
    • 2021-06-11
    • 2018-09-12
    • 2022-08-10
    • 2014-05-27
    相关资源
    最近更新 更多