如何从Thymeleaf中的Java方法插入片段

时间:2019-05-13 10:51:30

标签: thymeleaf

我有一个静态方法,该方法返回一个字符串,并且包含th:insert,如下面的示例所示。

尝试过

Java方法

public static String renderFragment() {
    return "<div th:insert=\"fragments/Text.html :: TextFragment\"></div>";
    //return "Text";
}

sample.html

<div class="container">
  <div class="row">
  <div th:with="WidgetName1=${T(com.configuration.helper.UiHelper).renderFragment()}">
    <!-- <div th:replace="@{'fragments/' + ${WidgetName1}} :: TextFragment"></div> is working if I return only widget name instead of html element --> 
    <div th:utext="${WidgetName1}"></div>  <!-- Some how it's not rendering actual fragment. It's showing as a plain text as <div th:insert="fragments/Text.html :: TextFragment"></div> -->
  </div>
  </div>
</div>

Text.html(片段)

<div th:fragment="TextFragment">
<h1>TEST</h1>
</div>

请让我知道更多详细信息。谢谢

0 个答案:

没有答案