【发布时间】:2021-01-26 23:30:39
【问题描述】:
我正在使用带有 Thymeleaf 的 Spring Boot 2.3.3.RELEASE。从 Thymeleaf 文档中,我不清楚何时必须在 Thymeleaf 表达式中使用 ${} 分隔符。
例如,我的模板中有这个,它正在工作:
<div th:title="${doTest() ? 'foo' : 'bar'}" />
但后来我重新阅读了文档,发现我可以使用这个:
<div th:title="${doTest()} ? 'foo' : 'bar'" />
但是? … : … 三元运算符是一个运算符,所以整个事情都是一个表达式。那么为什么我不需要${} 围绕整个属性值呢?
我也可以这样做吗?
<div th:title="doTest() ? 'foo' : 'bar'" />
这个怎么样?
<div th:title="'foo'" />
为什么是一个而不是另一个?
这个呢?我可以只使用一个裸露的true吗?
<div th:if="true">…</div>
或者我需要表达式分隔符吗?
<div th:if="${true}">…</div>
这是另一个例子:我在文档中看到了这个:
<li th:text="${item.description}" …
但是我可以只使用th:text="item.description" 吗?牙套对我有什么作用?
我也看到了:
th:each="item : ${items}"
我可以改用th:each="item : items" 吗?为什么没有 ${items} 中的大括号,Thymeleaf 不知道评估 items?
当我需要${} 时,肯定有一些简单的规则,但这并不是立即显而易见的。
【问题讨论】:
标签: spring spring-boot thymeleaf