Java 使用“包含来自同一thymeleaf模板的片段”;这是“内容”;或“是”:内容“;没有按预期工作
我包含了来自同一模板文件的模板片段。文件第“”节规定: “::domselector”或“此::domselector”包含来自 相同的模板 如果我没有误解,我应该能够如下引用该片段:Java 使用“包含来自同一thymeleaf模板的片段”;这是“内容”;或“是”:内容“;没有按预期工作,java,thymeleaf,Java,Thymeleaf,我包含了来自同一模板文件的模板片段。文件第“”节规定: “::domselector”或“此::domselector”包含来自 相同的模板 如果我没有误解,我应该能够如下引用该片段:th:include=“this::contentB”或th:include=“::contentB” 但只有充分引用th:include=“test fragment::contentB”对我来说才有效 这是示例代码: HomeController.java test.html 但我仍然不知道为什么在我的测试中会
th:include=“this::contentB”
或th:include=“::contentB”
但只有充分引用th:include=“test fragment::contentB”
对我来说才有效
这是示例代码:
HomeController.java
test.html
但我仍然不知道为什么在我的测试中会发生这种情况
编辑:
我也问过。也许是个虫子。我已经打开了一个。如果有人以同样的问题结束这里,这在
thymeleaf 2.1.4.版本中是不可能的,但它将在下一个版本中3.0
在这里可以找到一个全面的解释。我在一个普通的Spring MVC上尝试了它,该MVC运行在一个普通的tomcat 7容器中。你的代码运行良好。我无法复制你的问题。。。奇怪是的,真的很奇怪。我还尝试在tomcat-7.0.57和tomcat-8.0.32中部署为war,结果相同。我想这与thymeleaf-spring4和ViewResolver有关。我的thymeleaf和thymeleaf-spring4版本是2.1.4.0版本。
@Controller
class HomeController {
@RequestMapping({ "/", "index", "home" })
String index(Model model) {
return "test";
}
}
<!DOCTYPE html>
<html>
<head></head>
<body>
<ul th:replace="test-fragment :: contentA" />
</body>
</html>
<!DOCTYPE html>
<html>
<head></head>
<body>
<ul th:fragment="contentA">
<li th:each="idx : ${#numbers.sequence(1,4)}" th:switch="${idx}">
<th:block th:case="1"
th:include="test-fragment :: contentB"
th:with="strVar = 'One'"/>
<th:block th:case="2"
th:include="this :: contentB"
th:with="strVar = 'Two'"/>
<th:block th:case="3"
th:include=" :: contentB"
th:with="strVar = 'Three'"/>
<th:block th:case="*"
th:include="/test-fragment :: contentB"
th:with="strVar = 'Other'"/>
</li>
</ul>
<span th:fragment="contentB">
<span th:text="|value: ${strVar}|" />
</span>
</body>
</html>
<!DOCTYPE html>
<html>
<head></head>
<body>
<ul>
<li><span>value: One</span></li>
<li></li>
<li></li>
<li><span>value: Other</span></li>
</ul>
</body>
</html>
/* 186 */ String targetTemplateName = getTemplateName();
if (targetTemplateName == null) {
if (context != null && context instanceof Arguments) {
targetTemplateName = ((Arguments)context).getTemplateName();
/* 190 */ } else {
throw new TemplateProcessingException(
"In order to extract fragment from current template (templateName == null), processing context " +
"must be a non-null instance of the Arguments class (but is: " +
(context == null? null : context.getClass().getName()) + ")");
/* 195 */ }
}