开发者

Wicket: getModelObject returns null

I am in trouble with getModelObject. It is returning null. Actually the model concept is not clear to me. The code is:

public class SpaceCheckListWindow extends WebPage {
private SpaceCheckListForm spaceCheckListForm;
private Page parentPage;
private ModalWindow modalWindow;

public SpaceCheckListWindow(Page parentPage, final ModalWindow modalWindow) {
    this.parentPage = parentPage;
    this.modalWindow = modalWindow;

    String[] labels = new String[] {
            "a", 
            "b",
            "c",
            "d",
            "e",
            "f"
    };

    List<ListMemeber> list = new ArrayList<ListMemeber>();

    for(String label : labels) {
        list.add(new ListMemeber(label));
    }

    addComponent(list);
}   

private void addComponent(List<ListMemeber> list) {
    spaceCheckListForm = new SpaceCheckListForm("form", list);
    add(spaceCheckListForm);
}

private class SpaceCheckListForm extends Form {

    private static final long serialVersionUID = 1L;

    public SpaceCheckListForm(String id, final List<ListMemeber> list) {
        super(id);

        ListView listView = new ListView("listView", list) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(ListItem listItem) {
                ListMemeber member = (ListMemeber) listItem.getModelObject();
                listItem.add(new Label("label", member.getLabel()));
                listItem.add(new CheckBox("checkbox", new PropertyModel(member, "selected")));
            }
        };

        listView.setReuseItems(true);
        add(listView);

        AjaxButton submitBtn = new Aj开发者_JAVA百科axButton("submitBtn", this) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form form) {
                System.out.println(spaceCheckListForm.getModelObject());
                modalWindow.close(target);
            }
        };

        add(submitBtn);
    }

}

private class ListMemeber implements Serializable {
    private String label;
    private Boolean selected = Boolean.FALSE;

    public ListMemeber(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public Boolean getSelected() {
        return selected;
    }

    public void setSelected(Boolean selected) {
        this.selected = selected;
    }

    @Override
    public String toString() {
        return label + Boolean.toString(selected);
    }
}

}

Now onClick the AjaxButton it is returning null. I want to get the labels and corresponding checkbox values. What should I do to get it? Any information will be very helpful to me. Thank you.


Your component hierarchy looks like this:

SpaceCheckListWindow  
 SpaceCheckListForm (id: form)
  ListView (id: listView)
   ListItem (id: <generated by wicket>)
    Label (id: label)
    CheckBox (id: checkbox)
  AjaxButton (id: submitBtn)

In Wicket, each component may have a model, which stores data it displays/manipulates. This model is usually passed to the component in the constructor. If this doesn't happen, the component is initialised with an empty model (which is not an error in itself). But in your code, your form is created with an empty model and there's nothing to update the model either, so getModelObject() will always return null. This again is not an error, your form doesn't have to have a model object itself.

The only component which has a non-trivial model here is CheckBox, which reads/updates the selected field of ListMemeber, as it should. (Update: Of course your Label has a non-empty model too, but labels only read their model, never update it.)

So to sum it up: there's nothing fundamentally wrong with this code, you're just looking for the result in the wrong place: instead of logging out the model object of the form, list the selected fields of all the list members to see what's changing when you submit your form.

Some additional reading about Wicket models: Working with Wicket models

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜