Java 8 Optionals和JSP

时间:2017-09-01 09:02:04

标签: java jsp optional spring-form

有没有人知道如何在JSP上使用Optional对象?

我有一个班级:

@Entity
@Table(name = "projects")
public class Project {

@Expose
@ManyToOne
@JoinColumn(name = "customer_id")
private Customer endCustomer;
....

public Optional<Customer> getEndCustomer() {
    return Optional.ofNullable(endCustomer);
}

public void setEndCustomer(Customer endCustomer) {
    this.endCustomer = endCustomer;
}
....
}

我有jsp:

&#13;
&#13;
<td>
   <form:select class="form-control" id="endCustomer" path="endCustomer.id" tabindex="4">
       <form:options items="${endCustomers}" itemValue="id" itemLabel="name"/>
   </form:select>
</td>
&#13;
&#13;
&#13;

这部分原因并不明显:path="endCustomer.id"

有解决方法吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

尝试这样的事情

<td>
   <form:select class="form-control" id="endCustomer" path="${endCustomer.get().id}" tabindex="4">
       <form:options items="${endCustomers}" itemValue="id" itemLabel="name"/>
   </form:select>
</td>

相关问题How to access an object