VAADIN23 COMBOBOX.SETVALUE失败,转换错误
我正在使用VAADIN23使用粘合剂在Combobox中显示时区的列表:
ComboBox<ZoneId> timezoneField = new ComboBox<>();
timezoneField.setItemLabelGenerator(zoneId -> zoneId.getId());
timezoneField.setItems(Timezones.getZones());
binder.forField(timezoneField)
.bind("timezone");
final var registration = new Registration(member, organisation);
binder.setBean(registration);
上面的代码似乎正常工作,直到我尝试设置默认值:
UI.getCurrent().getPage().retrieveExtendedClientDetails(extendedClientDetails ->
{
int timezoneOffest = extendedClientDetails.getRawTimezoneOffset();
var possibleZones = Conversions.timezonesFromOffset(timezoneOffest);
if (possibleZones.size() != 0)
{
// this line throws
this.timezoneField.setValue((ZoneId) possibleZones.get(0));
}
});
FossibleZones返回ZoneID列表,但在调试器中它们显示为ZonereGion的列表。 我认为这是可以的,因为区域是一种ZoneDID。 当调用setValue时,我会做铸件,以防万一。
因此,当我致电时:
this.timezoneField.setValue((ZoneId) possibleZones.get(0));
抛出以下错误:
com.vaadin.flow.data.binder.BindingException: An exception has been thrown inside binding logic for the field element [suppress-template-warning='', _inputElementValue='', _clientSideFilter='false', selectedItem='null', invalid='false', pageSize='50', itemValuePath='key', itemIdPath='key', value='1']
at com.vaadin.flow.data.binder.Binder$BindingImpl.execute(Binder.java:1542) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.Binder$BindingImpl.doConversion(Binder.java:1286) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.Binder$BindingImpl.doValidation(Binder.java:1306) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.Binder$BindingImpl.validate(Binder.java:1247) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.Binder.lambda$doWriteIfValid$3(Binder.java:2245) ~
...
at com.vaadin.flow.data.binder.Binder.doWriteIfValid(Binder.java:2246) ~[flow-data-23.0.4.jar:23.0.4]
....
com.vaadin.flow.component.internal.AbstractFieldSupport.setValue(AbstractFieldSupport.java:135) ~[flow-server-23.0.4.jar:23.0.4]
at com.vaadin.flow.component.AbstractField.setValue(AbstractField.java:181) ~[flow-server-23.0.4.jar:23.0.4]
at com.vaadin.flow.component.combobox.ComboBox.setValue(ComboBox.java:396) ~[vaadin-combo-box-flow-23.0.5.jar:?]
at dev.onepub.ui.views.noauth.RegistrationView.lambda$4(RegistrationView.java:171) ~[classes/:?]
...
caused by:
Caused by: java.lang.ClassCastException: Cannot cast java.time.ZoneRegion to java.lang.String
at java.lang.Class.cast(Class.java:3889) ~[?:?]
at com.vaadin.flow.data.converter.Converter.lambda$from$957be2b0$1(Converter.java:104) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.Result.of(Result.java:90) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.converter.Converter.lambda$from$b652e465$1(Converter.java:104) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.converter.Converter$1.convertToModel(Converter.java:130) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.converter.Converter$2.lambda$convertToModel$6b579330$1(Converter.java:165) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.SimpleResult.flatMap(SimpleResult.java:65) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.ValidationResultWrap.flatMap(ValidationResultWrap.java:67) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.converter.Converter$2.convertToModel(Converter.java:165) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.Binder$BindingImpl.lambda$doConversion$0(Binder.java:1288) ~[flow-data-23.0.4.jar:23.0.4]
我注意到转换在抱怨将划分转换为字符串。
鉴于Combobox是类型ZoneID,并且具有ItemLabelgenerator,我不知道为什么可以生成此错误?
I'm using vaadin23 to display a list of timezones in a combobox using a binder:
ComboBox<ZoneId> timezoneField = new ComboBox<>();
timezoneField.setItemLabelGenerator(zoneId -> zoneId.getId());
timezoneField.setItems(Timezones.getZones());
binder.forField(timezoneField)
.bind("timezone");
final var registration = new Registration(member, organisation);
binder.setBean(registration);
The above code seems to work fine until I try to set a default value:
UI.getCurrent().getPage().retrieveExtendedClientDetails(extendedClientDetails ->
{
int timezoneOffest = extendedClientDetails.getRawTimezoneOffset();
var possibleZones = Conversions.timezonesFromOffset(timezoneOffest);
if (possibleZones.size() != 0)
{
// this line throws
this.timezoneField.setValue((ZoneId) possibleZones.get(0));
}
});
possibleZones returns a list of ZoneId but in the debugger they show as ZoneRegion's.
I think this is OK as ZoneRegion is a type of ZoneId.
I do the cast when calling setValue just in case.
So when I call:
this.timezoneField.setValue((ZoneId) possibleZones.get(0));
The following error is thrown:
com.vaadin.flow.data.binder.BindingException: An exception has been thrown inside binding logic for the field element [suppress-template-warning='', _inputElementValue='', _clientSideFilter='false', selectedItem='null', invalid='false', pageSize='50', itemValuePath='key', itemIdPath='key', value='1']
at com.vaadin.flow.data.binder.Binder$BindingImpl.execute(Binder.java:1542) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.Binder$BindingImpl.doConversion(Binder.java:1286) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.Binder$BindingImpl.doValidation(Binder.java:1306) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.Binder$BindingImpl.validate(Binder.java:1247) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.Binder.lambda$doWriteIfValid$3(Binder.java:2245) ~
...
at com.vaadin.flow.data.binder.Binder.doWriteIfValid(Binder.java:2246) ~[flow-data-23.0.4.jar:23.0.4]
....
com.vaadin.flow.component.internal.AbstractFieldSupport.setValue(AbstractFieldSupport.java:135) ~[flow-server-23.0.4.jar:23.0.4]
at com.vaadin.flow.component.AbstractField.setValue(AbstractField.java:181) ~[flow-server-23.0.4.jar:23.0.4]
at com.vaadin.flow.component.combobox.ComboBox.setValue(ComboBox.java:396) ~[vaadin-combo-box-flow-23.0.5.jar:?]
at dev.onepub.ui.views.noauth.RegistrationView.lambda$4(RegistrationView.java:171) ~[classes/:?]
...
caused by:
Caused by: java.lang.ClassCastException: Cannot cast java.time.ZoneRegion to java.lang.String
at java.lang.Class.cast(Class.java:3889) ~[?:?]
at com.vaadin.flow.data.converter.Converter.lambda$from$957be2b0$1(Converter.java:104) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.Result.of(Result.java:90) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.converter.Converter.lambda$from$b652e465$1(Converter.java:104) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.converter.Converter$1.convertToModel(Converter.java:130) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.converter.Converter$2.lambda$convertToModel$6b579330$1(Converter.java:165) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.SimpleResult.flatMap(SimpleResult.java:65) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.ValidationResultWrap.flatMap(ValidationResultWrap.java:67) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.converter.Converter$2.convertToModel(Converter.java:165) ~[flow-data-23.0.4.jar:23.0.4]
at com.vaadin.flow.data.binder.Binder$BindingImpl.lambda$doConversion$0(Binder.java:1288) ~[flow-data-23.0.4.jar:23.0.4]
I note that the conversion is complaining about converting ZoneRegion to String.
Given the combobox is of type ZoneId and has a ItemLabelGenerator I dont' see why this error can be generated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,答案是创建一个转换器。
对于我为什么使用与组合框相同的类型,SetValue需要转换器需要转换器,这仍然是没有意义的。
在这里记录是转换器:
并称呼它
So the answer was to create a convertor.
It still doesn't make sense to my why setValue needs a convertor to work given that it is using the same type as the combo box is storing.
For the record here is the convertor:
and to call it