【问题标题】:Spring Boot: Thymeleaf not resolving fragments after packagingSpring Boot:Thymeleaf 打包后不解析片段
【发布时间】:2014-10-21 18:36:37
【问题描述】:

我使用这样的片段:

@RequestMapping(value="/fragment/nodeListWithStatus", method= RequestMethod.GET)
public String nodeListWithStatus(Model model) {

    // status der nodes
    model.addAttribute("nodeList", nodeService.getNodeListWithOnlineStatus());

    return "/fragments :: nodeList";
}

模板位于 /src/main/resources/templates。从 IntelliJ 启动应用程序时,这工作正常。 一旦我创建了一个 .jar 并启动它,上面的代码就不再起作用了。错误:

[2014-10-21 20:37:09.191] log4j - 7941 ERROR [http-nio-666-exec-2] --- TemplateEngine: [THYMELEAF][http-nio-666-exec-2] Exception processing template "/fragments": Error resolving template "/fragments", template might not exist or might not be accessible by any of the configured Template Resolvers

当我用 winrar 打开 .jar 时,我看到 /templates/fragments.html - 所以它似乎在那里。

我的 pom.xml 有这部分用于构建 jar(Maven 清理,安装):

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>de.filth.Application</mainClass>
                <layout>JAR</layout>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

谁能告诉我我在这里做错了什么?

谢谢!

【问题讨论】:

    标签: spring fragment spring-boot thymeleaf


    【解决方案1】:

    您不需要在视图名称上以 / 开头,即您应该返回 fragments :: nodeList 而不是 /fragments :: nodeList。进行此更改后,Thymeleaf 在从 IDE 或 jar 文件运行时应该能够找到模板。

    如果你有兴趣,下面是幕后发生的事情:

    视图名称用于在类路径中搜索资源。 fragments :: nodeList 表示资源名称为/templates/fragments.html/fragments :: nodeList 表示资源名称为/templates//fragments.html(注意双斜杠)。当您在 IDE 中运行时,资源可以直接从文件系统中使用,并且双斜杠不会导致问题。当您从 jar 文件运行时,资源嵌套在该 jar 中,双斜杠会阻止它被发现。我不完全理解为什么会有这种行为差异,这是相当不幸的。我已经opened an issue,以便我们(Spring Boot 团队)可以查看是否有任何事情可以使行为保持一致。

    【讨论】:

    • 非常感谢安迪的解释,它解决了我的问题!让我们看看是否可以在框架中更深入地解决这个问题。
    【解决方案2】:

    这是一个老话题,但我在遇到类似症状和不同根本原因的问题时偶然发现了它。想分享对我有帮助的解决方案,以防它可以帮助其他人......

    messages.properties 文件的名称显然区分大小写,但并非无处不在。我有我的名为“Messages.properties”(大写M),它在IDE(IntelliJ)内部工作得很好,但是一旦我尝试从jar运行应用程序,所有消息都被替换为??parameter.name??。用小写的 m 代替 M 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2017-08-17
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2011-03-15
      • 2018-04-07
      • 2018-03-05
      相关资源
      最近更新 更多