【发布时间】:2013-08-21 19:43:09
【问题描述】:
我做了一些测试,看看如何将组合框绑定到某个 bean 属性,但我遇到了一个异常:“ConversionException:无法在......处将值转换为字符串” 我的示例在组合框的 indexedContainer 上工作正常,但我在使用 BeanItem 容器时遇到了一些问题。 是)我有的: 1.TestCountry,BeanItemContainer的简单java bean(为了简单起见,这里不放setter和getter或者constructor):
public class TestCountry implements Serializable {
private String name;
private String shortName;
}
-
BeanItemContainer 的实例化
BeanItemContainer<TestCountry> _data = new BeanItemContainer<TestCountry>(TestCountry.class); _data.addItem(new TestCountry("Afganistan","AF")); _data.addItem(new TestCountry("Albania","AL")); -
bean 归档组。这里的 TestBean 是另一个具有简单字符串属性的 bean ("firstName","phone","contry")
BeanFieldGroup<TestBean> binder = new BeanFieldGroup<TestBean>(TestBean.class); -
组合框
ComboBox myCombo = new ComboBox("Select your country", _data); -
基本代码
binder.bind(myCombo, "country");
当我尝试提交活页夹时,我收到关于将问题转换为字符串的错误。根据我阅读书籍和 vaadin api 的理解,BeanItemContainer 使用 bean 本身作为标识符,并且(这里可能是错误的)binder 使用项目标识符来绑定属性。所以这就是问题,从 Bean 到字符串的转换。 我尝试在我的 TestCountry bean 上实现 toString()、hash() 和 equals(),但没有成功。在这种情况下使用 BeanItemContainer 可以做什么?
提前致谢!!
【问题讨论】: