Java 8 Optionals and JSP

2.1k Views Asked by At

Does anybody have an idea how to use Optional objects on JSP?

I have a class:

@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;
}
....
}

And i have jsp:

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

this part doesn't work for obvious reasons: path="endCustomer.id"

is there a workaround? Thanks!

1

There are 1 best solutions below

3
On

Try something like this

<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>

Related question How to access an object