Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Model, PropertyModel and CompoundPropertyModel in Wicket?

Tags:

wicket

I have started learning Wicket framework and I came across wicket models and I read about Model(), CompouneModel() and CompoundPropertyModel() but I didn't get the actual difference between them. I searched on Google for this, but I didn't get any information about this.

This link gives description between CompoundPropertyModel and PropertyModel but still its not clear properly for difference.

Only the thing I could distinguish between Model and other two is model can not work with dynamic fields while other two can.

Can someone please give the difference between these models?

like image 246
Maddy Avatar asked May 13 '15 09:05

Maddy


2 Answers

All of them are implementations of the interface IModel.

Class Model is a basic implementation, that is almost just a 'data holder' so you can store an object in that model and get. The added value of this class is to forward to get and to set model object if the stored object is an other model (IModel).

Class PropertyModel is useful if you want to get / to set a property using property expression. See an example:

class Data {

    private Integer data;
    private String name;

    /* getters and setters */

}

How to get and set data using the PropertyModel:

Data data = new Data();
data.setId(1);
data.setName("data entity");
IModel idModel = new PropertyModel(data, "id");
IModel nameModel = new PropertyModel(data, "name");

System.out.println(data.getId());
// prints '1'
System.out.println(idModel.getObject());
// prints '1'
System.out.println(data.getName);
// prints 'data entity'
System.out.println(nameModel.getObject());
// prints 'data entity'


data.setId(2);
nameModel.setObject("a new name");

System.out.println(data.getId());
// prints '2'
System.out.println(idModel.getObject());
// prints '2'
System.out.println(data.getName());
// prints 'a new name'
System.out.println(nameModel.getObject());
// prints 'a new name'

Class CompoundPropertyModel is useful if you want to propage properties to the components by their IDs. See an example (using the same class Data):

Java Code (MyPanel.java):

class MyPanel extends Panel {

    public MyPanel(IModel<Data> model) {
        super(new CompoundPropertyModel<Data>(model));
        add(new Label("id"));
        add(new Label("data"));
    }
}

Markup (MyPanel.html):

<wicket:panel>
    <span wicket:id="id">placeholer for id</span>
    <span wicket:id="name">placeholer for id</span>
</wicket:panel>

Java Code using MyClass:

// in a Page, Panel or an other Component
Data data = new Data();
data.setId(3);
data.setName('my name');
add(new MyPanel(Model.of(data)));

Rendered output HTML (by the panel):

<span>3</span>
<span>my name</span>
like image 91
Martin Strejc Avatar answered Sep 21 '22 07:09

Martin Strejc


Please check https://cwiki.apache.org/confluence/display/WICKET/Working+with+Wicket+models. It gives very good explanations.
You can also read the official docs at http://ci.apache.org/projects/wicket/guide/6.x/guide/modelsforms.html

like image 39
martin-g Avatar answered Sep 19 '22 07:09

martin-g