Vaadin 8 在一种形式上使用多个活页夹的问题
Vaadin 8 Issue with using multiple binders on one form
我在 Vaadin 8 中将多个 java 对象绑定到一个表单时遇到问题。
我不确定代码中缺少什么。
下面是我的代码片段:
public Component getSiteForm(SiteRequest requestData, LocationRequest locationRequest, boolean isReadOnly) {
FormLayout siteForm = new FormLayout();
siteForm.setStyleName("outlined");
siteForm.setSizeFull();
DefaultFormBuilder defaultSiteFormBuilder = new DefaultFormBuilder(siteForm, isReadOnly);
final BeanValidationBinder<SiteRequest> siteBinder = new BeanValidationBinder<SiteRequest>(SiteRequest.class);
siteBinder.setBean(requestData);
final BeanValidationBinder<LocationRequest> locationBinder = new BeanValidationBinder<LocationRequest>(
LocationRequest.class);
locationBinder.setBean(locationRequest);
TextField nameField = defaultSiteFormBuilder.getTextField("Site Name");
siteBinder.bind(nameField, "name");
nameField.setWidth(SharkManagerConstant.DEFAULT_TEXT_FIELD_SIZE);
TextField zipcodeField = defaultSiteFormBuilder.getTextField("Zipcode");
locationBinder.forField(zipcodeField).withNullRepresentation("560047")
.withConverter(new StringToLongConverter("Please enter Zipcode")).bind("zipcode");
zipcodeField.setWidth(SharkManagerConstant.DEFAULT_TEXT_FIELD_SIZE);
return siteForm;
}
站点请求Class如下:
import java.io.Serializable;
import java.sql.Time;
import java.util.Date;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class SiteRequest implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@NotNull
@Size(min = 1, max = 40)
private String name;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
}
LocationRequestClass如下:
import java.io.Serializable;
import javax.validation.constraints.NotNull;
public class LocationRequest implements Serializable {
private static final long serialVersionUID = 1L;
private Long zipcode;
public Long getZipcode() {
return zipcode;
}
public void setZipcode(Long zipcode) {
this.zipcode = zipcode;
}
}
我在编辑表单时收到 ClassCastException。
虽然我已经把String转Long了。
不确定这是否是 Vaadin 中的错误。
我猜你的 withNullRepresentation
和 withConverter
的交互在这里是有问题的(withNullRepresentation
创建了一个特定的转换器,所以可能就是这样)。您可以尝试颠倒 withConverter
和 withNullRepresentation
调用的顺序,并使用 Long
(目标数据类型)而不是 String
作为空表示。
我在 Vaadin 8 中将多个 java 对象绑定到一个表单时遇到问题。
我不确定代码中缺少什么。
下面是我的代码片段:
public Component getSiteForm(SiteRequest requestData, LocationRequest locationRequest, boolean isReadOnly) {
FormLayout siteForm = new FormLayout();
siteForm.setStyleName("outlined");
siteForm.setSizeFull();
DefaultFormBuilder defaultSiteFormBuilder = new DefaultFormBuilder(siteForm, isReadOnly);
final BeanValidationBinder<SiteRequest> siteBinder = new BeanValidationBinder<SiteRequest>(SiteRequest.class);
siteBinder.setBean(requestData);
final BeanValidationBinder<LocationRequest> locationBinder = new BeanValidationBinder<LocationRequest>(
LocationRequest.class);
locationBinder.setBean(locationRequest);
TextField nameField = defaultSiteFormBuilder.getTextField("Site Name");
siteBinder.bind(nameField, "name");
nameField.setWidth(SharkManagerConstant.DEFAULT_TEXT_FIELD_SIZE);
TextField zipcodeField = defaultSiteFormBuilder.getTextField("Zipcode");
locationBinder.forField(zipcodeField).withNullRepresentation("560047")
.withConverter(new StringToLongConverter("Please enter Zipcode")).bind("zipcode");
zipcodeField.setWidth(SharkManagerConstant.DEFAULT_TEXT_FIELD_SIZE);
return siteForm;
}
站点请求Class如下:
import java.io.Serializable;
import java.sql.Time;
import java.util.Date;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class SiteRequest implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@NotNull
@Size(min = 1, max = 40)
private String name;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
}
LocationRequestClass如下:
import java.io.Serializable;
import javax.validation.constraints.NotNull;
public class LocationRequest implements Serializable {
private static final long serialVersionUID = 1L;
private Long zipcode;
public Long getZipcode() {
return zipcode;
}
public void setZipcode(Long zipcode) {
this.zipcode = zipcode;
}
}
我在编辑表单时收到 ClassCastException。 虽然我已经把String转Long了。
不确定这是否是 Vaadin 中的错误。
我猜你的 withNullRepresentation
和 withConverter
的交互在这里是有问题的(withNullRepresentation
创建了一个特定的转换器,所以可能就是这样)。您可以尝试颠倒 withConverter
和 withNullRepresentation
调用的顺序,并使用 Long
(目标数据类型)而不是 String
作为空表示。