我目前正在使用 vaadin 开发一个项目。到目前为止,除了本地化抽象选择组件(选项组、组合框、下拉列表等)之外,一切正常,其中选择项来自枚举类型。我采用了使用容器的方法:
private Container buildFundTransferTypeContainer() {
Container container = new IndexedContainer();
container.addContainerProperty("label", String.class, "");
container.addContainerProperty("value", FundTransferType.class, null);
for (FundTransferType type : FundTransferType.values()) {
Object id = container.addItem();
container.getContainerProperty(id, "label").setValue(
MessageResource.getLocalizedString(type.name()));
container.getContainerProperty(id, "value").setValue(type);
}
return container;
}
然后该字段设置为:
fundTransferTypeField.setContainerDataSource(buildFundTransferTypeContainer());
fundTransferTypeField.setItemCaptionPropertyId("label");
这样属性“label”将用于显示。到目前为止,这在浏览器中显示本地化值方面是有效的,但是我得到了
com.vaadin.data.Buffered$SourceException
...
Caused by: com.vaadin.data.Buffered$SourceException
at com.vaadin.ui.AbstractField.commit(AbstractField.java:261)
at com.vaadin.ui.Form.commit(Form.java:339)
... 34 more
Caused by: com.vaadin.data.Property$ConversionException: java.lang.NoSuchMethodException: net.novenix.tgsmango.core.enums.FundTransferType.<init>(java.lang.String)
at com.vaadin.data.util.MethodProperty.convertValue(MethodProperty.java:697)
at com.vaadin.data.util.MethodProperty.setValue(MethodProperty.java:666)
at com.vaadin.ui.AbstractField.commit(AbstractField.java:256)
... 35 more
Caused by: java.lang.NoSuchMethodException: com.sample.project.core.enums.FundTransferType.<init>(java.lang.String)
at java.lang.Class.getConstructor0(Class.java:2706)
at java.lang.Class.getConstructor(Class.java:1657)
at com.vaadin.data.util.MethodProperty.convertValue(MethodProperty.java:690)
... 37 more
当在表单上调用提交时。应如何在这些 vaadin 组件上处理本地化值。谢谢各位的回复
最佳答案
这是我使用的解决方案。 在字段工厂上或每当您创建字段时,
ComboBox accountStatusChoice = new ComboBox();
accountStatusChoice.setCaption(MessageResource.getLocalizedString("caption.accountStatus"));
accountStatusChoice.setRequired(true);
accountStatusChoice.setRequiredError(MessageResource.getLocalizedString("error.required.accountStatus"));
accountStatusChoice.setImmediate(true);
for (AccountStatus accountStatus : AccountStatus.values()) {
accountStatusChoice.addItem(accountStatus);
accountStatusChoice.setItemCaption(accountStatus, MessageResource.getLocalizedString(accountStatus.name()));
}
return accountStatusChoice;
在此代码中,MessageResource.getLocalizedString("resourceKey") 是我的 util 类,用于通过资源包检索本地化字符串。添加组合框(或您要使用的任何 AbstractSelect 组件)的内容时,使用 addItem 添加您要使用的对象值,并使用 setItemCaption(itemId, captionString) 设置对用户可见的标题。
您将用于表单的 beanItem 只需要具有 Enum 属性(在本例中为 AccountStatus 实例变量)。当在表单中调用提交时,将不再抛出 com.vaadin.data.Buffered$SourceException。
关于java - 枚举的 vaadin 本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8264360/