我有简单的实体(例如)
import java.util.Date;
class People implements Serializable{
...
private Date birthdate; //(getters, setters)
...}
UI代码:
final Binder<People> binder = new Binder<People>(People.class); ...
People bean=new People();
binder.setBean(bean);
DateField birthdate = new DateField("date of birth");
binder.bind(birthdate, "birthdate");
当我从UI中的日历中选择日期时,我得到:
Caused by: java.lang.ClassCastException: Cannot cast java.time.LocalDate to java.util.Date
at java.lang.Class.cast(Class.java:3369)
at com.vaadin.data.Binder$BindingBuilderImpl.lambda$createConverter$f6099586$1(Binder.java:800)
at com.vaadin.data.Converter.lambda$null$fdd4de71$1(Converter.java:105)
at com.vaadin.data.Result.of(Result.java:91)
我尝试使用
DateField birthdate = new DateField("birthdate");
binder.bind(birthdate, "birthdate");
binder.forField(birthdate).withConverter(new LocalDateToDateConverter());
但结果相同。 如何正确地将Date绑定到DateField?
答案 0 :(得分:2)
问题在于你如何使用binder
。而是尝试
DateField birthdate = new DateField("birthdate");
binder.forField(birthdate).withConverter(new LocalDateToDateConverter()).bind("birthdate");
forField
方法返回构建器设计模式后面的对象。这意味着你在该对象上调用一些(链式)方法,并通过调用bind
来完成它。