我正在嘗試保存“員工”模型而不為其分配任何“電話”模型(oneToOne 關系)。我有一個表格,我在其中通過添加以下內容來創建員工:名字,姓氏,然后是一個下拉列表,我可以從中選擇一個電話來分配它。每當我不選擇任何東西時,index.html 頁面都會拋出:Exception evaluating SpringEL expression: "employee.phone.getBrand() +' '+ employee.phone.getModel()" (template: "index" - line 31, col 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>我提到可以在數據庫中找到該對象并且 phone_id 為空。我想添加一些有電話的員工和一些沒有電話的員工。輸出應該是:Doe John noPhone -> nothing selected from dropdown list. Doe Jane iPhone 10s -> 手機已選中。你有什么建議嗎 ?
1 回答

躍然一笑
TA貢獻1826條經驗 獲得超6個贊
如果 phone 可以為 null,請嘗試使用 null-safe accessor ?.
。嘗試這樣的事情employee.phone?.getBrand()
添加回答
舉報
0/150
提交
取消