java - Wicket: getModelObject returns null -


i in trouble getmodelobject. returning null. model concept not clear me. 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 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);     } } 

}

now onclick ajaxbutton returning null. want labels , corresponding checkbox values. should it? information helpful me. thank you.

your component hierarchy looks this:

spacechecklistwindow    spacechecklistform (id: form)   listview (id: listview)    listitem (id: <generated wicket>)     label (id: label)     checkbox (id: checkbox)   ajaxbutton (id: submitbtn) 

in wicket, each component may have model, stores data displays/manipulates. model passed component in constructor. if doesn't happen, component initialised empty model (which not error in itself). in code, form created empty model , there's nothing update model either, getmodelobject() return null. again not error, form doesn't have have model object itself.

the component has non-trivial model here checkbox, reads/updates selected field of listmemeber, should. (update: of course label has non-empty model too, labels read model, never update it.)

so sum up: there's nothing fundamentally wrong code, you're looking result in wrong place: instead of logging out model object of form, list selected fields of list members see what's changing when submit form.

some additional reading wicket models: working wicket models


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -