如何将字段绑定到bean vaadin中的外键

时间:2014-04-22 08:15:33

标签: java web frameworks vaadin

我的豆子就是这个

   public class RegistrationEntity 
 {
private Long accountNumber;
private String firstName;
private String middleName;
private String lastName;


// occupation entity is another table
private OccupationEntity occupationEntity;
}

组件就像这样

 firstName = new TextField("First Name");
    firstName.setRequired(true);
    firstName.setWidth(COMMON_FIELD_WIDTH);



    lastName = new TextField("Last Name");
    lastName.setRequired(true);
    lastName.setWidth(COMMON_FIELD_WIDTH);


     occupation = new NativeSelect("Occupation:");
    occupation.setRequired(true);
    occupation.setImmediate(true);
    occupation.setWidth(COMMON_FIELD_WIDTH);
    occupation.setNullSelectionItemId("--Select--");
    occupation.addItem("Static");

只要考虑firstName和lastName就可以像这样绑定

  fieldgroup.bind(firstName,"firstName");

但是如何将占用NativeSelect绑定到OccupEntity?

1 个答案:

答案 0 :(得分:2)

在这种情况下,您可以使用 BeanFieldGroup 和自定义FieldGroupFieldFactory:

binder = new BeanFieldGroup<RegistrationEntity>(RegistrationEntity.class);
binder.setFieldFactory(new FieldGroupFieldFactory() {
            private DefaultFieldGroupFieldFactory defaultFactory = new DefaultFieldGroupFieldFactory();
            @Override
            public <T extends Field> T createField(Class<?> dataType, Class<T> fieldType) {
                if ("OccupationEntity".equals(dataType.getName())) {
                    BeanItemContainer<OccupationEntity> occupationEntityContainer  = new BeanItemContainer<OccupationEntity>(OccupationEntity.class);
                    occupationEntityContainer.addAll(allEntities);
                    Field field = new Combobox("Occupation:", occupationEntityContainer);
                    ((Combobox) field).setItemCaptionPropertyId(--Insert caption bean property here--);
                    ((Combobox) field).setNullSelectionAllowed(false);
                    return fieldType.cast(field);
                }

                return defaultFactory.createField(dataType, fieldType);
            }
        });

binder.setItemDataSource(item);