【问题标题】:org.thymeleaf.exceptions.TemplateInputException: Error resolving fragment: template or fragment could not be resolvedorg.thymeleaf.exceptions.TemplateInputException:解析片段时出错:无法解析模板或片段
【发布时间】:2017-06-29 23:43:57
【问题描述】:

我有一个奇怪的问题,当我使用替换或包含时,我的一些 html 文件没有被包含在内。什么会导致这个?

header.html

 <header id="header" xmlns:th="http://www.thymeleaf.org">
       <div th:replace="/blocks/topbar :: topbar"></div>
 </header>

topbar.html

<div class="top-bar">
    <div class="container">
        <div class="row">
            <div class="col-sm-6 col-xs-4">
                <div class="top-number"><p><i class="fa fa-phone-square"></i>  +0123 456 70 90</p></div>
           </div>
         <div class="col-sm-6 col-xs-8">
            <div class="social">
                <ul class="social-share">
                    <li><a href="#"><i class="fa fa-facebook"></i></a></li>
                </ul>
            </div>
        </div>
      </div>
    </div><!--/.container-->
</div><!--/.top-bar-->

错误:

 org.thymeleaf.exceptions.TemplateInputException: Error resolving fragment: "~{'/blocks/topbar' :: topbar}": template or fragment could not be resolved (template: "blocks/header" - line 3, col 10)

【问题讨论】:

    标签: java spring-boot thymeleaf


    【解决方案1】:

    您的topbar.html 中没有th:fragment="topbar"。 额外的&lt;div&gt; 可能会解决您的问题。

    <div th:fragment="topbar">
      <!--fragment div start-->
      <div class="top-bar">
        <div class="container">
          <div class="row">
            <div class="col-sm-6 col-xs-4">
              <div class="top-number">
                <p><i class="fa fa-phone-square"></i> +0123 456 70 90</p>
              </div>
            </div>
            <div class="col-sm-6 col-xs-8">
              <div class="social">
                <ul class="social-share">
                  <li><a href="#"><i class="fa fa-facebook"></i></a>
                  </li>
                </ul>
              </div>
            </div>
          </div>
        </div>
        <!--/.container-->
      </div>
      <!--/.top-bar-->
    </div>
    <!--fragment div end-->

    【讨论】:

    • 这解决了我的问题,但是为什么其他包含没有片段表达式的工作,而有些则没有?
    • 这个我不能准确地说,因为没有表达我永远无法让它工作。
    【解决方案2】:

    您并不总是需要 th:fragment,您可以使用“.top-bar”而不是“topbar”:

    <div th:replace="/blocks/topbar :: .top-bar"></div>
    

    类选择器是完全有效的,您在百里香中还有更多选择器的可能性,请参见此处:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-c-markup-selector-syntax

    【讨论】:

      猜你喜欢
      • 2021-10-19
      • 2016-12-21
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      相关资源
      最近更新 更多