【问题标题】:How to fix: Exception evaluating SpringEL expression如何修复:评估 SpringEL 表达式的异常
【发布时间】:2019-08-01 07:35:39
【问题描述】:

我正在尝试保存“员工”模型而不为其分配任何“电话”模型(oneToOne 关系)。我有一个表格,在其中我通过添加来创建员工:名字,姓氏,然后是一个下拉列表,我可以从中选择一个电话来分配它。每当我不选择任何内容时,index.html 页面都会抛出: 评估 SpringEL 表达式的异常:“employee.phone.getBrand() +' '+ employee.phone.getModel()”(模板:“index” - 第 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>
            &nbsp &nbsp
            <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 为空。我想添加一些有电话的员工和一些没有电话的员工。 输出应该是: Doe John noPhone -> 从下拉列表中未选择任何内容。 Doe Jane iPhone 10s -> 已选择手机。

你有什么建议吗?

【问题讨论】:

    标签: java html


    【解决方案1】:

    如果 phone 可以为 null,请尝试使用 null-safe 访问器 ?.。试试这样的employee.phone?.getBrand()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 2023-01-03
      • 2019-05-24
      • 2018-02-20
      • 1970-01-01
      • 2019-08-30
      • 2017-11-12
      相关资源
      最近更新 更多