无法在Springboot应用程序中检索模板

时间:2017-02-06 15:08:05

标签: java tomcat spring-boot

我的索引文件的正文如下所示:

<div class="container" style="width: 100% !important;">

    <!--Header-->
    <div th:replace="fragments/header :: header"></div>

    <!--Content-->
    <!--<div th:replace="@{'fragments/' + ${template}} :: ${template}"></div>-->

    <!--Footer-->
    <div th:replace="fragments/footer :: footer"></div>

</div>

似乎在抱怨我的标题评论下的这一行,但我不确定为什么。会发生什么事?

这是我的错误:

Mon Feb 06 16:06:26 CET 2017
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving fragment: "~{'fragments/header' :: header}": template or fragment could not be resolved (template: "index" - line 34, col 10)

例外:

org.thymeleaf.exceptions.TemplateInputException: Error resolving fragment: "~{'fragments/header' :: header}": template or fragment could not be resolved (template: "index" - line 34, col 10)
    at org.thymeleaf.standard.expression.FragmentExpression.resolveExecutedFragmentExpression(FragmentExpression.java:593) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.thymeleaf.standard.processor.AbstractStandardFragmentInsertionTagProcessor.computeFragment(AbstractStandardFragmentInsertionTagProcessor.java:368) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.thymeleaf.standard.processor.AbstractStandardFragmentInsertionTagProcessor.doProcess(AbstractStandardFragmentInsertionTagProcessor.java:111) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:593) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:337) ~[thymeleaf-spring5-3.0.3.M1.jar:3.0.3.M1]
    at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:186) ~[thymeleaf-spring5-3.0.3.M1.jar:3.0.3.M1]
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1271) ~[spring-webmvc-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037) ~[spring-webmvc-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980) ~[spring-webmvc-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:964) ~[spring-webmvc-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:856) ~[spring-webmvc-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:841) ~[spring-webmvc-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at

2 个答案:

答案 0 :(得分:2)

是否有带

的文件模板/ fragments / header.html
< ... th:fragment="header">

在classpath中?

答案 1 :(得分:1)

我不确定你能做到这一点:

<!--<div th:replace="@{'fragments/' + ${template}} :: ${template}"></div>-->

或者可以通过前缀和后缀加双下划线进行预处理。

即使被注释掉,Thymeleaf也会处理这条线。