Java 8 可选项和 JSP

标签 java jsp option-type 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:

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

由于显而易见的原因,这部分不起作用:path="endCustomer.id"

有解决办法吗? 谢谢!

最佳答案

尝试这样的事情

<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

关于Java 8 可选项和 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45996695/

相关文章:

java - 在创建 fragment 之前更改 fragment 时应用程序崩溃

java - 如何防止 JodaTime 期间的负面结果

java - 使用 ant 将 jsps 预编译为 Jetty8 的类

java - 我的方法有什么问题?

java - 方法必须返回 int

java - JspWriter 写入与打印

java - 刷新jsp页面时再次提交

c# - BinaryFormatter 中可选字段的反序列化

filter - java.util.NoSuchElementException : No value present even though we use stream

swift - 如何使具有可选属性的自定义类符合 "hashable"协议(protocol)