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

I started to study the Wicket structure and I came across gates and I read about Model (), CompouneModel () and CompoundPropertyModel (), but I did not get the actual difference between them. I searched on Google for this, but I did not get any information about this.

This link gives a description between CompoundPropertyModel and PropertyModel, but is still unclear for the difference.

Only that I could distinguish between the model and the other two cannot work with dynamic fields, while the other two can.

Can someone comment on the difference between these models?

+6
source share
2 answers

All of them are implementations of the IModel interface.

The Model class is a basic implementation, which is almost a "data holder", so you can save the object in this model and get it. The added value of this class is to forward, to receive and set the model object, if the stored object is another model (IModel).

The PropertyModel class is useful if you want to get / set a property using a property expression. Example:

class Data { private Integer data; private String name; /* getters and setters */ } 

How to get and set data using 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' 

The CompoundPropertyModel class is useful if you want to skip component properties by their identifiers. See Example (using the same Data class):

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))); 

HTML output is displayed (in the panel):

 <span>3</span> <span>my name</span> 
+9
source
+1
source

Source: https://habr.com/ru/post/987114/


All Articles