Thymeleaf在Docker容器中运行时无法解析模板

时间:2019-07-26 13:44:00

标签: spring-boot docker docker-compose thymeleaf

我有一个适用于SpringBoot应用程序的标准项目结构: src/main/java for src files and src/main/resources

当我这样构建和运行我的应用程序时:

mvn clean install
mvn spring-boot:run

一切正常。

当我尝试在docker-compose指定的docker容器中运行

file:
version: "3.1"
services:
  app:
    container_name: thdnes-app
    restart: always
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - "8080:8080"

和Dockerfile:

FROM openjdk:11-jdk
VOLUME /tmp
COPY /target/thdnes-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]

SpringBoot可以正常启动(通过控制台输出判断,没有错误)。

但是当我导航到 http://localhost:8080/login 时,我得到一个错误:

thdnes-app | 2019-07-26 13:32:11.361 ERROR 1 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "/login.html": Error resolving template [/login.html], template might not exist or might not be accessible by any of the configured Template Resolvers
thdnes-app | 
thdnes-app | org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/login.html], template might not exist or might not be accessible by any of the configured Template Resolvers
thdnes-app |    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
thdnes-app |    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
thdnes-app |    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
thdnes-app |    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
thdnes-app |    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) ~[thymeleaf-spring5-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
thdnes-app |    at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) ~[thymeleaf-spring5-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
thdnes-app |    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.17.jar!/:9.0.17]
thdnes-app |    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.17.jar!/:9.0.17]
...

经测试,不涉及Docker,该模板存在并且包装在一个jar中,但是为什么它在容器内失败是我的问题。

1 个答案:

答案 0 :(得分:1)

当我通过Maven在本地启动时,我遇到了同样的问题

mvn spring-boot:run

一切正常,但是当我从docker容器中运行时,百里香没有找到模板。

解决方案是从视图名称的开头删除/,如下例所示:

之前

    public String findOwners(Model model) {
        model.addAttribute("owner", new Owner());
        return "/owners/findOwners";
    }

之后

    public String findOwners(Model model) {
        model.addAttribute("owner", new Owner());
        return "owners/findOwners";
    }