Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot th:更换不带';t:Thymeleaf中的th:Thymeleaf不能很好地工作_Spring Boot_Thymeleaf - Fatal编程技术网

Spring boot th:更换不带';t:Thymeleaf中的th:Thymeleaf不能很好地工作

Spring boot th:更换不带';t:Thymeleaf中的th:Thymeleaf不能很好地工作,spring-boot,thymeleaf,Spring Boot,Thymeleaf,根据特里梅拉夫的说法 片段可以包括任何th:*属性。一旦片段包含到目标模板中(具有th:insert/th:replace属性的模板),这些属性将被评估,并且它们将能够引用此目标模板中定义的任何上下文变量 我的碎片 <div th:fragment="link"> <a th:href="@{${url}}"><span th:inline="text">[[${text}]]</span></a> </div>

根据特里梅拉夫的说法

片段可以包括任何th:*属性。一旦片段包含到目标模板中(具有th:insert/th:replace属性的模板),这些属性将被评估,并且它们将能够引用此目标模板中定义的任何上下文变量

我的碎片

<div th:fragment="link">
    <a th:href="@{${url}}"><span th:inline="text">[[${text}]]</span></a>
</div>
为什么
th:replace
不起作用而
th:inlcude
工作正常

注意:
th:insert
超出范围,因为我使用的是
Thymeleaf v2.1.5
似乎是一个类似(不相同)的问题,感谢中提到的问题

作为此问题的解决方法,您仍然可以使用
th:include
,然后使用
th:remove=“tag”
删除额外的
div
,类似于:

<div th:include="fragments/common :: link" th:with="url='www.google.com', text='Click Me'" th:remove="tag"></div>

原因是
th:replace
实际上删除了当前标记,因此您丢失了其中的所有属性,但从片段中获取了所有属性。在您的情况下,这意味着您从未在范围中定义任何
th:with
变量。
th:include
的工作方式正好相反。您可以释放片段标记,但保留布局中定义的所有内容

考虑一下这个片段:

。。。
和布局:


结果是:

一些文本

一些文本
是,但根据官方文件th:replace应该可以使用。是吗?@AbdullahKhan它的工作原理就像你引用的官方文件的哪一部分中描述的一样?就是我在问题中发布的那一部分@AbdullahKhan我认为您在问题中引用的部分是关于片段如何工作以及您可以在片段中使用th:*属性。比如
您可以在片段中使用任何th:*属性,它们将看到主模板范围
基本上,这部分文档说明,AMLEAF将首先执行
th:insert/th:replace
,就像我在上面提供的链接中描述的那样,然后将所有内容作为一个大模板使用公共模板进行评估scope@AbdullahKhan 这些属性将在片段包含到目标模板中后进行评估,但如果使用
th:replace
则在包含过程中会从主模板布局中的标记中释放所有属性。谢谢,但我知道这一点。
<a href="">
    <span>null</span>
</a>
<a href="www.google.com">
    <span>Click Me</span>
</a>
<div th:include="fragments/common :: link" th:with="url='www.google.com', text='Click Me'" th:remove="tag"></div>