我遇到了getModelObject問題。它返回null。其實模型概念對我來說並不清楚。該代碼是:Wicket:getModelObject返回null
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 AjaxButton("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);
}
}
}
現在的onClick的AjaxButton它返回null。我想獲取標籤和相應的複選框值。我該怎麼做才能得到它?任何信息對我都很有幫助。謝謝。
感謝您的詳細情況進行說明。我看到模型通常是POJO。現在如果我想創建一個這樣的類作爲表單的模型。我如何插入一個列表視圖到它?對於文本框或複選框,它將是字符串或布爾值,不是!那麼,listview應該如何聲明一個列表以及它的getter和setter?那麼什麼是toString方法?在上面的問題,如果我打電話listview.getmodelobject然後它的工作。感謝你們對我的幫助。 – 2011-02-18 15:51:34