- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据Thymeleaf docs
Fragments can include any th:* attributes. These attributes will be evaluated once the fragment is included into the target template (the one with the th:insert/th:replace attribute), and they will be able to reference any context variables defined in this target template.
我的片段
<div th:fragment="link">
<a th:href="@{${url}}"><span th:inline="text">[[${text}]]</span></a>
</div>
这就是我添加它的方式。
<div th:replace="fragments/common :: link" th:with="url='www.google.com', text='Click Me'"></div>
我得到的html
<a href="">
<span>null</span>
</a>
但是,与 th:include
一样可以正常工作,并给出以下 HTML。
<a href="www.google.com">
<span>Click Me</span>
</a>
为什么th:replace
不工作而th:inlcude
工作正常?
注意: th:insert
超出范围,因为我使用的是 Thymeleaf v2.1.5
最佳答案
原因是 th:replace
实际上删除了当前标签,因此您丢失了其中的每个属性,但从片段中获取了所有属性。在您的情况下,这意味着您从未在范围内定义任何 th:with
变量。th:include
的工作方式相反。您失去了片段标签,但保留了布局中定义的所有内容。
考虑这个片段:
<fragmenttag th:fragment="link" style="background-color: red">...</fragmenttag>
和布局:
<layouttag th:include="fragments/common :: link" style="font-size: 250%;"/>
<layouttag th:replace="fragments/common :: link" style="font-size: 250%;"/>
结果是:
<layouttag style="font-size: 250%;">Some Text</layouttag>
<fragmenttag style="background-color: red">Some Text</fragmenttag>
如果你想使用th:replace
,因为fragment中有一些重要的属性,你可以在布局中的某些父标签中定义你需要的一切。
<body th:with="url='www.google.com', text='Click Me'">
<div th:replace="fragments/common :: link" ></div>
</body>
<小时/>
您在帖子中引用了文档:
Fragments can include any th:* attributes. These attributes will be evaluated once the fragment is included into the target template (the one with the th:insert/th:replace attribute), and they will be able to reference any context variables defined in this target template.
我在这里没有看到任何矛盾,因为文档的这一部分是关于 th:* 属性在片段内。
Fragments (th:fragment part) can include any th:* attributes.
在您的问题中,您正在讨论丢失目标模板中定义的 th:* 属性。但无论如何,这部分是相当困难的,你首先执行包含逻辑
These attributes will be evaluated once the fragment is included
这里没有任何内容可以让您假设您将获得在目标模板或片段主标记中定义的所有内容,因为它们都可以根据您要使用的女巫包含策略进行替换(th:insert/th:替换)。
因此,您定义了 th:with="url='www.google.com', text='Click Me'"
属性,但它从未包含在最终结果模板中,因为您选择了th:replace
包含策略,因此 th:with 属性从未被评估,并且范围内没有 url
和 text
变量。这里没有矛盾。
关于spring-boot - 第:replace doesn't work well with th:with in Thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47032018/
我正在从一个旧数据库中选择数据,该数据库有一个滥用状态列。状态列中包含多条信息。值如“新联系人 YYYY”、“在线 YYYY”、“更新 YYYY”、“撤回 YYYY”等……。您可能已经猜到了,YYYY
我想使用ant的replace任务替换这样的文件之一中的 token : 版本.txt version.number=${versionNumber} build.gradle task writeV
sorted_elems 列表中有一些元素将被更改为 str 例如: sorted_elems = ['[abc]', '[xyz]', ['qwe']] 我想删除定义的字符 - [, ], ' 并打
如果替换在变量中传递,第一次和第二次替换是否等效? #!/usr/bin/env perl6 use v6; my $foo = 'switch'; my $t1 = my $t2 = my $t3
我正在做一个本地测试来比较 C# 中 String 和 StringBuilder 的 Replace 操作性能,但是对于 String 我使用了以下代码: String str = "String
我看到了所有内容,没有看到使用“--replace”的方式的内容。 如何在 wkhtmltopdf 中使用“--replace”。 请给我一个例子,谢谢。:) 最佳答案 假设您有一个页脚 my_foo
我需要在 50 多个文件中进行大量搜索/替换,并且正在使用 Sublime Text 3。 有没有办法逐步执行并交互确认每个更改?我不想要只执行所有替换的一揽子“全部替换”操作。 我正在思考 vi/v
那么“replace ”属性如何与 composer 一起使用? ?我已经阅读了 Composer 文档,但仍然不明白。搜索更多信息并没有回答我的问题。 当我查看Laravel/Framework上的
我在玩 F# 句法。 在瑞典,我们有一个游戏叫做“Backslang”(谷歌翻译自“Rövarspråk”) 规则很简单。你说的所有话都必须以特定的方式说出来。虽然人声是相同的,但每个辅音都必须用“o
是否可以在Atom或Sublime Text中对“item”一词使用“查找和替换”并将其替换为“item [i]”,其中[i]是从1开始的数字? 我有一堆看起来像这样的物品: item1 item1
我不太确定如何解释,所以我将从输出开始。我需要返回这个: { replies: [ { type: 'text', content: 'one' } { type:
我是 C# 的新手,所以请原谅我的错误。 我想在每次调用该段代码时(每次调用 2-4 次)替换字符串的某些部分。我想知道哪种方法在性能方面更好:string.Replace 或 stringBuild
我了解.Replace() 和-replace 之间的区别,但是什么是-replace 和[Regex]: :替换()? 我测试了以下 2 个代码,但对我来说结果完全一样。 我还引用了 PowerSh
如果我正在做类似的事情: someString.Replace("abc","").Replace("def","").Replace(@"c:\Windows","") 我怎样才能把它替换成 Reg
我是 C# 的新手,所以请原谅我的错误。 我想在每次调用该段代码时(每次调用 2-4 次)替换字符串的某些部分。我想知道哪种方法在性能方面更好:string.Replace 或 stringBuild
当我使用 call() 或 apply() 时,我遇到了问题。 console.log(String.prototype.replace === String.replace);//false 我认为
在我使用 5-10 个替换的情况下是否有必要使用 stringbuilder。 String someData = "......"; someData = someData.replaceAll("
我有一个字符串,我需要用字典中的值替换其中的标记。它必须尽可能高效。使用 string.replace 进行循环只会消耗内存(记住,字符串是不可变的)。 StringBuilder.Replace()
我正在使用具有许多不同库依赖项的 gradle 项目并使用新的 list 合并。在我的 标签我已经这样设置了: .... 但我收到错误消息: /android/MyApp/app/src/main
我正在尝试从一个数据库中的表中提取模式并将其传输到另一个数据库。以下是执行此操作的函数的一部分。 当 Table( ... ) 时出错被称为。 功能示例: def transfer_data(self
我是一名优秀的程序员,十分优秀!