我会这样做:
<span th:if="${employee.dateOfTermination.before(#dates.createNow())}">Case 1</span>
<span th:if="${employee.dateOfTermination.after(#dates.createNow())}">Case 2</span>
或者,如果您确实喜欢这个开关:
<div th:switch="${employee.dateOfTermination.before(#dates.createNow())}">
<span th:case="true">Case 1</span>
<span th:case="false">Case 2</span>
</div>
另一种选择是在后端插入一个新参数并将其传递给 Thymeleaf。 例如,您在后端插入参数“age”,其 getter 根据与现在时间的比较返回相应的字符串(例如“OLDER”或“YOUNGER”)。然后将其添加到相应 Thymeleaf 页面的 ModelView 中,您可以在其中使用 th:switch:
<th:block th:switch="${age}">
<th:block th:case="OLDER">
<span th:text="Age is older than now" style="..."></span>
</th:block>
<th:block th:case="YOUNGER">
<span th:text="Age is younger than now" style="..."></span>
</th:block>
</th:block>