显示 属性 项目名称而不是 id
Show property item name instead of id
我的布局包含多个组件(4 个文本字段和 2 个组合框)。
这是如何定义的:
private ManufacturerRepository manRepo;
InteriorDoor interiorDoor;
Manufacturer manufacturer;
ComponentHelper componentHelper;
@PropertyId("width")
private TextField widthField;
@PropertyId("height")
private TextField heightField;
@PropertyId("thickness")
private TextField thicknessField;
@PropertyId("modelName")
private TextField modelName;
@PropertyId("hasGlass")
private ComboBox glassCombo;
@PropertyId("manufacturerId")
private ComboBox manCombo;
private BeanItemContainer<Manufacturer> beanItemContainer;
beanItemContainer
用于从数据库中获取数据:
beanItemContainer = new BeanItemContainer<>(Manufacturer.class, manRepo.findAll());
manCombo.setContainerDataSource(beanItemContainer);
不介意 glassCombo
因为它有常量数据并且绑定在那里工作得很好。
这是我绑定字段的方式:
// binding data to fields
BeanFieldGroup binder = new BeanFieldGroup<>(InteriorDoor.class);
binder.setItemDataSource(interiorDoor);
binder.setBuffered(false);
binder.bindMemberFields(this);
到目前为止一切顺利,但是当它发生在布线领域时,manCombo
出现了大问题-从组合框中选择项目时,我收到了这个
Caused by: com.vaadin.data.util.converter.Converter$ConversionException: Could not convert value to Long
at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:778) ~[vaadin-server-7.7.5.jar:7.7.5]
at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:755) ~[vaadin-server-7.7.5.jar:7.7.5]
at com.vaadin.ui.AbstractField.setValue(AbstractField.java:539) ~[vaadin-server-7.7.5.jar:7.7.5]
... 49 common frames omitted
Caused by: com.vaadin.data.util.converter.Converter$ConversionException: Unable to convert value of type .domain.Manufacturer to model type class java.lang.Long. No converter is set and the types are not compatible.
at com.vaadin.data.util.converter.ConverterUtil.convertToModel(ConverterUtil.java:182) ~[vaadin-server-7.7.5.jar:7.7.5]
at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:775) ~[vaadin-server-7.7.5.jar:7.7.5]
... 51 common frames omitted
请帮忙
P.S 我试图转换为 (((Manufacturer).getValue).getId()
但它没有效果
更新 1
@Marco C 解决方案不起作用。现在调用布局我收到这个:
java.lang.NullPointerException: null at
java.lang.Class.isAssignableFrom(Native Method) ~[na:1.8.0_121] at
com.vaadin.data.util.converter.ConverterUtil.canConverterPossiblyHandle(ConverterUtil.java:243)
~[vaadin-server-7.7.5.jar:7.7.5] at
com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:649)
~[vaadin-server-7.7.5.jar:7.7.5] at
com.vaadin.data.fieldgroup.FieldGroup.bind(FieldGroup.java:272)
~[vaadin-server-7.7.5.jar:7.7.5] at
com.vaadin.data.fieldgroup.BeanFieldGroup.bind(BeanFieldGroup.java:155)
~[vaadin-server-7.7.5.jar:7.7.5] at
com.vaadin.data.fieldgroup.FieldGroup.buildAndBindMemberFields(FieldGroup.java:1011)
~[vaadin-server-7.7.5.jar:7.7.5] at
com.vaadin.data.fieldgroup.FieldGroup.bindMemberFields(FieldGroup.java:856)
~[vaadin-server-7.7.5.jar:7.7.5] at
com.reborn.doorshop.ui.components.InteriorEditor.editEntity(InteriorEditor.java:156)
~[classes/:na] at
com.reborn.doorshop.ui.views.InteriorView.lambda$initComponents446b05(InteriorView.java:53)
~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) ~[na:1.8.0_121] at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
更新2:
manCombo.setConverter(new Converter<Object, Long>() {
@Override
public Long convertToModel(Object value, Class<? extends Long> targetType, Locale locale)
throws ConversionException {
if (value == null) {
return null;
}
return ((Manufacturer)value).getId();
}
@Override
public Object convertToPresentation(Long value, Class<?> targetType, Locale locale)
throws ConversionException {
if (value == null) {
return null;
}
return beanItemContainer.getItemIds().stream().filter( m -> value.equals(m.getId()))
.findFirst().orElse(null);
}
@Override
public Class<Long> getModelType() {
return null;
}
@Override
public Class<Object> getPresentationType() {
return null;
}
});
您可以实现 Converter Manufacturer -> Long 并将其设置在组合框上
manCombo.setConverter(new Converter<Object, Long>() {
@Override
public Long convertToModel(Object value, Class<? extends Long> targetType, Locale locale) throws ConversionException {
if (value == null) {
return null;
}
return ((Manifacturer)value).getId();
}
@Override
public Object convertToPresentation(Long value, Class<?> targetType, Locale locale) throws ConversionException {
if (value == null) {
return null;
}
return beanItemContainer.getItemIds().stream().filter( m -> value.equals(m.getId()))
.findFirst().orElse(null);
}
...
});
我的布局包含多个组件(4 个文本字段和 2 个组合框)。 这是如何定义的:
private ManufacturerRepository manRepo;
InteriorDoor interiorDoor;
Manufacturer manufacturer;
ComponentHelper componentHelper;
@PropertyId("width")
private TextField widthField;
@PropertyId("height")
private TextField heightField;
@PropertyId("thickness")
private TextField thicknessField;
@PropertyId("modelName")
private TextField modelName;
@PropertyId("hasGlass")
private ComboBox glassCombo;
@PropertyId("manufacturerId")
private ComboBox manCombo;
private BeanItemContainer<Manufacturer> beanItemContainer;
beanItemContainer
用于从数据库中获取数据:
beanItemContainer = new BeanItemContainer<>(Manufacturer.class, manRepo.findAll());
manCombo.setContainerDataSource(beanItemContainer);
不介意 glassCombo
因为它有常量数据并且绑定在那里工作得很好。
这是我绑定字段的方式:
// binding data to fields
BeanFieldGroup binder = new BeanFieldGroup<>(InteriorDoor.class);
binder.setItemDataSource(interiorDoor);
binder.setBuffered(false);
binder.bindMemberFields(this);
到目前为止一切顺利,但是当它发生在布线领域时,manCombo
出现了大问题-从组合框中选择项目时,我收到了这个
Caused by: com.vaadin.data.util.converter.Converter$ConversionException: Could not convert value to Long at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:778) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:755) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.ui.AbstractField.setValue(AbstractField.java:539) ~[vaadin-server-7.7.5.jar:7.7.5] ... 49 common frames omitted Caused by: com.vaadin.data.util.converter.Converter$ConversionException: Unable to convert value of type .domain.Manufacturer to model type class java.lang.Long. No converter is set and the types are not compatible. at com.vaadin.data.util.converter.ConverterUtil.convertToModel(ConverterUtil.java:182) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:775) ~[vaadin-server-7.7.5.jar:7.7.5] ... 51 common frames omitted
请帮忙
P.S 我试图转换为 (((Manufacturer).getValue).getId()
但它没有效果
更新 1
@Marco C 解决方案不起作用。现在调用布局我收到这个:
java.lang.NullPointerException: null at java.lang.Class.isAssignableFrom(Native Method) ~[na:1.8.0_121] at com.vaadin.data.util.converter.ConverterUtil.canConverterPossiblyHandle(ConverterUtil.java:243) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:649) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.data.fieldgroup.FieldGroup.bind(FieldGroup.java:272) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.data.fieldgroup.BeanFieldGroup.bind(BeanFieldGroup.java:155) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.data.fieldgroup.FieldGroup.buildAndBindMemberFields(FieldGroup.java:1011) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.data.fieldgroup.FieldGroup.bindMemberFields(FieldGroup.java:856) ~[vaadin-server-7.7.5.jar:7.7.5] at com.reborn.doorshop.ui.components.InteriorEditor.editEntity(InteriorEditor.java:156) ~[classes/:na] at com.reborn.doorshop.ui.views.InteriorView.lambda$initComponents446b05(InteriorView.java:53) ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
更新2:
manCombo.setConverter(new Converter<Object, Long>() {
@Override
public Long convertToModel(Object value, Class<? extends Long> targetType, Locale locale)
throws ConversionException {
if (value == null) {
return null;
}
return ((Manufacturer)value).getId();
}
@Override
public Object convertToPresentation(Long value, Class<?> targetType, Locale locale)
throws ConversionException {
if (value == null) {
return null;
}
return beanItemContainer.getItemIds().stream().filter( m -> value.equals(m.getId()))
.findFirst().orElse(null);
}
@Override
public Class<Long> getModelType() {
return null;
}
@Override
public Class<Object> getPresentationType() {
return null;
}
});
您可以实现 Converter Manufacturer -> Long 并将其设置在组合框上
manCombo.setConverter(new Converter<Object, Long>() {
@Override
public Long convertToModel(Object value, Class<? extends Long> targetType, Locale locale) throws ConversionException {
if (value == null) {
return null;
}
return ((Manifacturer)value).getId();
}
@Override
public Object convertToPresentation(Long value, Class<?> targetType, Locale locale) throws ConversionException {
if (value == null) {
return null;
}
return beanItemContainer.getItemIds().stream().filter( m -> value.equals(m.getId()))
.findFirst().orElse(null);
}
...
});