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

3

8

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?

Cherycherye answered 13/5, 2015 at 9:28 Comment(0)
P
12

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>
Periodontics answered 13/5, 2015 at 9:53 Comment(0)
F
1

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

Fiduciary answered 13/5, 2015 at 10:48 Comment(0)
P
0

Very good example. Just need some correction:

According to

IModel idModel = new PropertyModel(data, "id");

and

private Integer data;

I expect that In Data class supposed to be

private Integer id;

instead.

And the same mistake in next place

 add(new Label("data"));

expect

add(new Label("name"));

to make everything correct. Thank you for your help.

Performative answered 19/10, 2018 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.