我有代码:
<div
th:each="annotsField : ${annotsFields}"
th:with="someVariable='admin/editElementsFromObjects/fragments/templateFor'+${annotsField.type()}+' :: templateFor'+${annotsField.type()}"
>
<div th:text="'!!!'+${someVariable}+'!!!'" />
<!--<div th:replace="${someVariable}" />-->
<!--<div th:replace="admin/editElementsFromObjects/fragments/templateForString :: templateForString" />-->
<br />
<br />
<br />
如您所见,此代码将在页面上打印出类似!!! admin / editElementsFromObjects / fragments / templateForString :: templateForString !!! 。好。
但现在我们将取消注释代码
<div th:replace="admin/editElementsFromObjects/fragments/templateForString :: templateForString" />
它将包含现有的模板文件。好。好。
但是为什么当我创建类似
的代码时<div
th:each="annotsField : ${annotsFields}"
th:with="someVariable='admin/editElementsFromObjects/fragments/templateFor'+${annotsField.type()}+' :: templateFor'+${annotsField.type()}"
>
<div th:replace="${someVariable}" />
<br />
<br />
<br />
</div>
然后我得到了异常
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template "admin/editElementsFromObjects/fragments/templateForString :: templateForString", template might not exist or might not be accessible by any of the configured Template Resolvers (admin/editElementsFromObjects/editForm:24)
为什么它不能包含在代码中
<div th:replace="${someVariable}" />
但包含OK代码
<div th:replace="admin/editElementsFromObjects/fragments/templateForString :: templateForString" />
并且我没有看到my变量值的错误?..
答案 0 :(得分:2)
我刚尝试使用Thymeleaf 2.1.4,如果你使用预处理器表达式引用replace
模板,它就可以了:
<div th:replace="__${someVariable}__" />