如果我使用
在我的主机上运行应用程序,我的应用程序工作正常mvn spring-boot:run
但是当我将它部署到 docker 上时,它不起作用并且出现此错误:
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/store/index", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/store/index", template might not exist or might not be accessible by any of the configured Template Resolvers
但是如果我继续访问由 spring security 控制的 url: http:localhost:8080/login,则可以正常呈现模板。似乎是权限问题,但我不确定。
这是我的 Dockerfile:
FROM openjdk
VOLUME /tmp
RUN touch engdevcommerce.jar
COPY target/engdevcommerce.jar engdevcommerce.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/engdevcommerce.jar"]
解决方案: 原来我遇到的问题与 docker 部署无关。我在我的主机上运行了 jar 文件,但我遇到了同样的错误。 问题是,在 Controller 方法中返回 View url 的地方,我是从这样的斜杠开始的: "/.../..."。当应用程序打包为 .jar 文件时,spring 不会加载带有双斜杠的 View 。我必须在我返回 ModelAndView 的每个位置以及我的 html 文件上的 th:insert 标记处删除 url 开头的斜杠字符。
这个链接对我帮助很大: spring-boot-thymeleaf-not-resolving-fragments-after-packaging
最佳答案
始终首先使用“java -jar your_app_name.jar”命令检查应用程序执行情况! 一般来说,这个问题主要通过检查以下 3 点来解决-
- 您的 application.properties 应该有条目- spring.thymeleaf.prefix=classpath:/templates/
- 您的 Controller 应该返回模板的名称,前面没有任何斜杠。由于 thymeleaf 配置区分大小写。
- 您的项目应遵循标准的 spring boot thymeleaf 目录结构。如果不是,则在您的 application.properties 文件中进行相应的更改。
关于spring boot 在 docker 上部署时找不到模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52130925/