使用“this :: content”或“:: content”包含来自同一百里香模板的片段无法按预期工作

Tob*_*ías 5 java thymeleaf

我包含来自同一模板文件的模板片段。文档的“ 8.1 包括模板片段 - 定义和引用片段”部分指出:

" ::domselector " 或 " this::domselector " 包含来自同一模板的片段。

如果我没有误解,我应该能够如下引用该片段:th:include="this :: contentB"或者th:include=":: contentB" 但只有完整的参考th:include="test-fragment :: contentB"对我有用。

这是示例代码:

HomeController.java

@Controller
class HomeController {
  @RequestMapping({ "/", "index", "home" })
  String index(Model model) {
    return "test";
  }
}
Run Code Online (Sandbox Code Playgroud)

测试.html

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <ul th:replace="test-fragment :: contentA" />
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

测试片段.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>
Run Code Online (Sandbox Code Playgroud)

输出是:

<!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>
Run Code Online (Sandbox Code Playgroud)

案例 2 和案例 3 缺失。我究竟做错了什么?

我正在使用Spring Boot 1.3.2.RELEASE进行测试

编辑:

我做了一个小测试项目来重现该问题,可以在这里找到: https: //github.com/t-cst/thymeleaf-springboot-test

编辑:

经过一些调试后,我发现当片段选择器为"this :: contentB"" :: contentB"时,模板名称被解析为test 而不是test-framgent. 这是在以下位置完成的:

StandardFragment#extractFragment:189来自 thymeleaf-2.1.4.RELEASE:

/* 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 */      }
           }
Run Code Online (Sandbox Code Playgroud)

但我仍然不知道为什么在我的测试中会发生这种情况。

编辑:

我也在thymeleaf论坛上问过。也许这是一个错误。我已经打开了一个问题

Tob*_*ías 3

如果有人以同样的问题结束这里,这在thymeleaf 2.1.4.RELEASE版本中是不可能的,但它将在下一个版本中3.0

全面的解释可以在这个问题评论中找到。