Saturday, July 03, 2010

Passing objects to Page constructors in wicket

When you pass data through models into Page constructors in wicket, you have to be careful about how to treat that data.

Do not store the underlying object that is returned by getObject() on the wicket page for many reasons. What can happen if you do this is duplicates in your database, issues with memory consumption as domain objects are stored in your wicket session, hibernate errors etc..

class Test {

private Object object;

public Test(IModel model){
   object=model.getObject();
}

public void doStuff(){
 IModel model=new LoadableDetachableModel(){
   public Object load(){
      return object;
}}
  ListView view=new ListView("list", model){
......
}}}
}


If you need to pass data into the page constructor, then store them as models and remember to manually detach them in onDetach().


public class Test {
    private IModel objectModel;
    
    public void onDetach(){
        super.onDetach();
        objectModel.detach();
    }

    public Test(IModel model) {
        objectModel = model;
    }


    public void doStuff() {

        ListView view = new ListView("list", objectModel) {
            ......
        };
    }
}
}

0 comments: