Which method is better?:
add(new Label("label", new PropertyModel<String>(cat, "name")));
or
add(new Label("label", cat.getName()));
I tried to find any information about comparison.. but couldn’t find anything
How I understand the first method is for read/write logic and the second for read only logic, (if I am not right please write me). But for read only logic which better is?
They’re functionally different.
The first one says: whenever this component is re-rendered, refresh the value. The second one says: display the value as it was at the time of creation.
Which one do you need? If you want a dynamically refreshing label, you have no choice, it’s
PropertyModelorCompoundPropertyModel(see later).If you want it to stay the same, even if the underlying object changes, you can’t use
PropertyModels.However, if you are absolutely sure that
cat.getName()is never going to change, and therefore the two versions behave the same way, I personally wouldn’t usePropertyModelfor three reasons:PropertyModelwill break.cat.getName()there, you can “click through” in your IDE, your label will show up in a search for all invocations of thegetName()method and so on.If you have many components referring to fields of the same object, you can consider using
CompoundPropertyModels, which, although still suffer from problems 1 and 2, make your code look a lot cleaner.If you have three or fewer components like this though and you don’t need a dynamic model, just use the modelless format.