春季靴子:Thymeleaf打包后无法分解碎片 [英] Spring Boot: Thymeleaf not resolving fragments after packaging

查看:106
本文介绍了春季靴子:Thymeleaf打包后无法分解碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这样的片段:

@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并启动它,上面的代码将不再起作用.错误:

The templates are in /src/main/resources/templates. This works fine when starting the application from IntelliJ. As soon as i create an .jar and start it, above code no longer works. Error:

[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-似乎就在那里.

When i open the .jar with winrar, i see /templates/fragments.html - so it seems to be there.

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

My pom.xml has this part for building the 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>

有人可以告诉我我在做什么错吗?

Can anyone tell me what im doing wrong here?

谢谢!

推荐答案

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

You don't need the leading / on the view name, i.e. you should return fragments :: nodeList rather than /fragments :: nodeList. Having made this change Thymeleaf should be able to find the template when run from your IDE or from a jar file.

如果您有兴趣,这是幕后故事:

If you're interested, here's what's happening under the hood:

视图名称用于在类路径上搜索资源. fragments :: nodeList表示资源名称为/templates/fragments.html/fragments :: nodeList表示资源名称为/templates//fragments.html(请注意双斜杠).在IDE中运行时,资源可直接用于文件系统,并且双斜杠不会造成问题.从jar文件运行时,资源嵌套在该jar中,并且双斜杠阻止找到它.我不完全理解为什么行为上会有这种差异,这是很不幸的.我打开了一个问题,以便我们(Spring Boot团队)可以查看是否有什么方法可以使行为保持一致.

The view name is used to search for a resource on the classpath. fragments :: nodeList means that the resource name is /templates/fragments.html and /fragments :: nodeList means that the resource name is /templates//fragments.html (note the double slash). When you're running in your IDE the resource is available straight off the filesystem and the double slash doesn't cause a problem. When you're running from a jar file the resource is nested within that jar and the double slash prevents it from being found. I don't fully understand why there's this difference in behaviour and it is rather unfortunate. I've opened an issue so that we (the Spring Boot team) can see if there's anything we can do to make the behaviour consistent.

这篇关于春季靴子:Thymeleaf打包后无法分解碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆