0

我正在尝试运行带有子编辑器的编辑器示例。刷新父级时,子编辑器的值为空。类是人员和地址。主编是:

    // editor fields
public TextField firstname;
public TextField lastname;
public NumberField<Integer> id;

public AddressEditor address = new AddressEditor();

public PersonEditor(Person p){
    asWidget();
}

@Override
public Widget asWidget() {
    setWidth(400);
    setBodyStyle("padding: 5px;");
    setHeaderVisible(false);

    VerticalLayoutContainer c = new VerticalLayoutContainer();

    id = new NumberField<Integer>(new IntegerPropertyEditor());
    // id.setName("id");
    id.setFormat(NumberFormat.getFormat("0.00"));
    id.setAllowNegative(false);
    c.add(new FieldLabel(id, "id"), new VerticalLayoutData(1, -1));

    firstname = new TextField();
    // firstname.setName("firstname");
    c.add(new FieldLabel(firstname, "firstname"), new VerticalLayoutData(1, -1));

    lastname = new TextField();
    lastname.setName("lastname");
    c.add(new FieldLabel(lastname, "lastname"), new VerticalLayoutData(1, -1));

    c.add(address);
    add(c);
    return this;

副主编:

public class AddressEditor extends Composite implements Editor<Address> {

private AddressProperties props = GWT.create(AddressProperties.class);
private ListStore<Address> store = new ListStore<Address>(props.key());
ComboBox<Address> address;

public AddressEditor() {
    for(int i = 0; i < 5; i ++)
        store.add(new Address("city" + i));
    address = new ComboBox<Address>(store, props.nameLabel());

    address.setAllowBlank(false);
    address.setForceSelection(true);
    address.setTriggerAction(TriggerAction.ALL);
    initWidget(address);
}

这是创建驱动程序的地方:

private HorizontalPanel hp;

private Person googleContact;
PersonDriver driver = GWT.create(PersonDriver.class);

public void onModuleLoad() {

    hp = new HorizontalPanel();
    hp.setSpacing(10);

    googleContact = new Person();
    PersonEditor pe = new PersonEditor(googleContact);

    driver.initialize(pe);
    driver.edit(googleContact);

    TextButton save = new TextButton("Save");
    save.addSelectHandler(new SelectHandler() {

        @Override
        public void onSelect(SelectEvent event) {
            googleContact = driver.flush();
            System.out.println(googleContact.getFirstname() + ", " + googleContact.getAddress().getCity());
            if (driver.hasErrors()) {
                new MessageBox("Please correct the errors before saving.").show();
                return;
            }
        }
    });

googleContact.getFirstname() 的值已填充,但 googleContact.getAddress() 始终为空。我错过了什么?

4

1 回答 1

1

AddressEditor需要映射到模型 -目前Address,它似乎没有,除非Address只有一个 getter/setter,称为getAddress()and setAddress(Address),这并没有多大意义。

如果您只想要一个ComboBox<Address>Editor<Address>已经实现),请考虑将该组合PersonEditor直接放入。否则,您需要添加@Path("")到该AddressEditor.address字段,以表明它应该直接编辑值本身,而不是子属性(即person.getAddress().getAddress())。

构建地址编辑器的另一种方法是AddressAddressEditor. 这是驱动程序默认情况下所期望的,因此当它看到一个名为“地址”的字段时会感到困惑。

关于代码本身的两个快速想法:无需将人传递给PersonEditor-这是驱动程序本身的工作。其次,您的编辑器字段不需要是public,它们就是不能private

于 2013-02-08T19:07:25.837 回答