Spring boot th:文本中的叶连接正在覆盖以前的值

Spring boot th:文本中的叶连接正在覆盖以前的值,spring-boot,thymeleaf,Spring Boot,Thymeleaf,我想连接两个字符串变量。这个要求非常简单,取决于条件,我只需要连接一个静态字符串“Out of office”。然而,当它被渲染时,只会显示“外出”,而不会显示以前的变量值,如first和last name。有什么想法吗 <option th:text="${assignee.getUserProfile().getFirstName() + ' ' + assignee.getUserProfile().getLastName() + assignee.getUserProfile().

我想连接两个字符串变量。这个要求非常简单,取决于条件,我只需要连接一个静态字符串“Out of office”。然而,当它被渲染时,只会显示“外出”,而不会显示以前的变量值,如first和last name。有什么想法吗

<option th:text="${assignee.getUserProfile().getFirstName() + ' ' + assignee.getUserProfile().getLastName() + assignee.getUserProfile().getOutOfOfficeApproval() != null? '(Out of office)':''}"/>

像这样:

<option th:text="${assignee.getUserProfile().getFirstName() + ' ' + assignee.getUserProfile().getLastName() + (assignee.getUserProfile().getOutOfOfficeApproval() != null? '(Out of office)':'')}"/>

您还可以使用th:with、内联和javabean格式改进格式。类似于其中一个:

<option th:with="profile=${assignee.userProfile}" th:text="${profile.firstName + ' ' + profile.lastName + (profile.outOfOfficeApproval != null? ' (Out of office)' : '')}">

或(w/Thymeleaf 3)


[${profile.firstName+''+profile.lastName}]]
[${profile.outOfOfficeApproval!=null?'(不在办公室)':'''''':'}]]

在语句周围添加括号。目前,整个concatted字符串用于
=比较。另外,您可能希望在控制器中执行此操作,而不是在视图中执行此操作,并在其中填充布尔值或消息。@M.Deinum您能告诉我如何在您所说的语句周围使用括号吗?只需将最后一部分用
()
包装即可,但正如建议的那样,我将把这个逻辑移到控制器上,而不是开始在HTML模板中编程。
<option th:with="profile=${assignee.userProfile}">
    [[${profile.firstName + ' ' + profile.lastName}]]
    [[${profile.outOfOfficeApproval != null? '(Out of office)' : ''}]]
</option>