gpt4 book ai didi

Spring 启动: Thymeleaf not resolving fragments after packaging

转载 作者:行者123 更新时间:2023-12-01 19:07:22 24 4
gpt4 key购买 nike

我使用这样的片段:

@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 clean,安装):

<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>

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

谢谢!

最佳答案

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

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

View 名称用于搜索类路径上的资源。 fragments::nodeList 表示资源名称为 /templates/fragments.html/fragments::nodeList 表示资源名称为 /templates//fragments.html (注意双斜杠)。当您在 IDE 中运行时,资源可以直接从文件系统获取,并且双斜杠不会引起问题。当您从 jar 文件运行时,资源嵌套在该 jar 中,并且双斜杠会阻止找到它。我不完全理解为什么会有这种行为差异,这是相当不幸的。我有opened an issue这样我们(Spring Boot 团队)就可以看看是否可以采取任何措施来使行为保持一致。

关于 Spring 启动: Thymeleaf not resolving fragments after packaging,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26493831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com