本文介绍了当我在括号中调用它时,Thymeleaf 不会解析我的应用程序中的片段.这是为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是春天和百里香的新手.我有一个带有 thymeleaf 的 spring-boot web 应用程序,我遇到了一个问题.这条线完美地工作:

I am new to spring and thymeleaf.I have a spring-boot web application with thymeleaf, and I encountered a problem.This line works perfectly:

<footer th:replace="fragments/footer :: footer">Footer</footer>

但是当我在它周围写上括号时,如 文档,出现错误.

But when I write brackets around it, as in the documentation, I get an error.

<footer th:replace="~{fragments/footer :: footer}">Footer</footer>

错误:

出现意外错误(类型=内部服务器错误,状态=500).解析模板~{fragments/footer"时出错,模板可能不存在或可能无法被任何配置的模板解析器访问(索引:11)

那可能是什么问题,我使用默认的spring boot配置.pom.xml :

So what can be the problem, I use the default spring boot configuration.pom.xml :

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

日志:

2017-03-01 22:47:51.585 ERROR 4014 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] 异常处理模板index":解析模板~{fragments/footer"时出错,模板可能不存在或可能无法被任何配置的模板解析器访问(索引:11)2017-03-01 22:47:51.591 错误 4014 --- [nio-8080-exec-1] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] 在上下文中路径[]抛出异常[请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateInputException:解析模板~{fragments/footer"时出错,模板可能不存在或可能无法被任何已配置的模板解析器(索引:11)] 访问,其根本原因是org.thymeleaf.exceptions.TemplateInputException:解析模板~{fragments/footer"时出错,模板可能不存在或可能无法被任何配置的模板解析器访问(索引:11)在 org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]在 org.thymeleaf.standard.fragment.StandardFragment.extractFragment(StandardFragment.java:202) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]在 org.thymeleaf.standard.processor.attr.AbstractStandardFragmentHandlingAttrProcessor.computeFragment(AbstractStandardFragmentHandlingAttrProcessor.java:72) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]在 org.thymeleaf.processor.attr.AbstractFragmentHandlingAttrProcessor.processAttribute(AbstractFragmentHandlingAttrProcessor.java:63) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]

推荐答案

看起来您正在使用 thymeleaf 2.片段表达式是 thymeleaf 3 的新功能.

Looks like you are using thymeleaf 2. Fragment expressions are new to thymeleaf 3.

http://www.thymeleaf.org/doc/articles/thymeleaf3migration.html#fragment-expressions

这篇关于当我在括号中调用它时,Thymeleaf 不会解析我的应用程序中的片段.这是为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 05:22