简体   繁体   中英

Java 8 Optionals and JSP

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM