首页 文章

Spring Boot:Thymeleaf在包装后没有解析碎片

提问于
浏览
2

即时通讯使用这样的片段:

@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,install):

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

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

谢谢!

2 回答

  • 3

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

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

    视图名称用于在类路径上搜索资源 . fragments :: nodeList 表示资源名称为 /templates/fragments.html/fragments :: nodeList 表示资源名称为 /templates//fragments.html (请注意双斜杠) . 当你're running in your IDE the resource is available straight off the filesystem and the double slash doesn' t导致问题 . 当你完全理解为什么我们(Spring Boot团队)能够看到我们能做些什么来使行为保持一致时 . [2532712_ t]完全理解为什么会有's this difference in behaviour and it is rather unfortunate. I' ve opened an issue .

  • 0

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

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

相关问题