th:with 中的 Thymeleaf 嵌套条件
Thymeleaf nested condition in th:with
我想做这样的总结:
<div id="header_nav" th:fragment="header_nav (someId)" th:with="navPrefix=${'/content' + (someId ? ('/'+someId) : '')"}">
稍后在 div 中将其用作 *{navPrefix}。我不知何故不明白条件嵌套。
谁能帮我?
谢谢!
我看到的唯一问题是末尾有额外的双引号,并且当您传入字符串时,您的三元运算符不包含布尔表达式。
以下工作用于显示块中的前缀或传递的参数:
<div id="header_nav"
th:fragment="header_nav(someId)"
th:with="navPrefix = ${'/content' + (someId != null ? ('/' + someId) : '')} ">
<div th:text="${someId}"></div>
<div th:text="${navPrefix}"></div>
</div>
我怀疑这是因为您在使用片段时使用了 th:include
。根据Thymeleaf Documentation th:include
简单的包含片段内容:
Whereas th:include will include the contents of the fragment into its host tag, th:replace will actually substitute the host tag by the fragment’s
如果您使用的是 th:include
,那么您的 th:with
将不会被包含在内,因此它将始终为空白。您可以在与 th:fragment
声明相同的元素中使用其他属性来观察这一点,例如 style
.
您可以使用 th:replace
来解决这个问题,这将交换整个片段,而不仅仅是内容。或者,您可以使用 th:block
包装您的片段内容,您可以使用它来定义 navPrefix
:
<div th:fragment="header_nav(someId)">
<th:block th:with="navPrefix = ${'/content' + (someId != null ? ('/' + someId) : '')} ">
... contents of fragment ...
</th:block>
</div>
我想做这样的总结:
<div id="header_nav" th:fragment="header_nav (someId)" th:with="navPrefix=${'/content' + (someId ? ('/'+someId) : '')"}">
稍后在 div 中将其用作 *{navPrefix}。我不知何故不明白条件嵌套。 谁能帮我? 谢谢!
我看到的唯一问题是末尾有额外的双引号,并且当您传入字符串时,您的三元运算符不包含布尔表达式。
以下工作用于显示块中的前缀或传递的参数:
<div id="header_nav"
th:fragment="header_nav(someId)"
th:with="navPrefix = ${'/content' + (someId != null ? ('/' + someId) : '')} ">
<div th:text="${someId}"></div>
<div th:text="${navPrefix}"></div>
</div>
我怀疑这是因为您在使用片段时使用了 th:include
。根据Thymeleaf Documentation th:include
简单的包含片段内容:
Whereas th:include will include the contents of the fragment into its host tag, th:replace will actually substitute the host tag by the fragment’s
如果您使用的是 th:include
,那么您的 th:with
将不会被包含在内,因此它将始终为空白。您可以在与 th:fragment
声明相同的元素中使用其他属性来观察这一点,例如 style
.
您可以使用 th:replace
来解决这个问题,这将交换整个片段,而不仅仅是内容。或者,您可以使用 th:block
包装您的片段内容,您可以使用它来定义 navPrefix
:
<div th:fragment="header_nav(someId)">
<th:block th:with="navPrefix = ${'/content' + (someId != null ? ('/' + someId) : '')} ">
... contents of fragment ...
</th:block>
</div>