我正在尝试保存"employee"模型而不为其分配任何"phone"模型(oneToOne关系)。我有一个表单,通过添加以下形式来创建员工:firstName,lastName,然后是一个下拉列表,从中可以选择要为其分配电话的电话。每当我不选择任何内容时,index.html页面将引发:评估SpringEL表达式的异常:"employee.phone.getBrand() +' '+ employee.phone.getModel()"(模板:"index"-第31行,第17栏)
<div align="center">
<table >
<thead>
<tr>
<th>Id Employee</th>
<th>Last Name</th>
<th>First Name</th>
<th>Phone</th>
<th>Manage</th>
</tr>
</thead>
<tbody>
<tr th:each="employee: ${employeeList} ">
<td th:text="${employee.id}">Id</td>
<td th:text="${employee.lastName}">Last Name</td>
<td th:text="${employee.firstName}">First Name</td>
<td th:text="${employee.phone.getBrand() +' '+ employee.phone.getModel()} ">Brand</td>
<td>
<a th:href="@{'employee/edit/' + ${employee.id}}"><button class="btnEdit"><i class="fa fa-edit"></i></button></a>
   
<a th:href="@{'employee/delete/' + ${employee.id}}"><button class="btnDelete"><i class="fa fa-edit"></i></button></a>
</td>
</tr>
</tbody>
</table>
</div>
我提到可以在DB中找到该对象,而phone_id为null。我想添加一些有电话的员工,有些没有电话的员工。 输出应为: Doe John noPhone->从下拉列表中未选择任何内容。 Doe Jane iPhone 10s->手机已选择。
你有什么建议吗 ?
分析解答
如果phone可以为空,请尝试使用null-safe访问器?.
。试试这样的employee.phone?.getBrand()