Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Code Block
public interface IModel extends IDetachable
{
  public Object getObject(final Component component);
  public void setObject(final Component component, final Object object);
}

And the base interface IDetachable looks like this:

...

The PropertyModel class allows you to create a model that accesses a particular property of its nested model object at runtime. This property is accessed using an expression language which has a syntax similar to OGNL (see http://www.ognl.org) (until version 1.1, Wicket actually did use the OGNL library, however it has been replaced by custom implementation). The simplest PropertyModel constructor is:

...

Looking at our example POJO, the property expression "name" can be used to access the "name" property of any Person object via the getName() getter method. Also, when updating a model, using property expression "name" has the effect of calling setName(String). Hence construct a property model to use our POJO, give it a property expression "name" and set it on a TextField, not only will that TextField display the current value of the "name" property, it will also update the "name" property with any user input provided.

Wiki MarkupMore complex property expressions are possible as well. For example, you can access sub-properties via reflection using a dotted path notation, which means the property expression {{"person.name"}} is equivalent to calling {{getPerson().getName()}} on the given model object. Arrays can be accessed as in "{{persons\[4\].name}}". As you can see, PropertyModels are quite a powerful way to give Wicket components access to model objects.

Info
titleMore information on the syntax

More information on the allowed property expressions can be found either in the wicket.util.lang.PropertyResolver JavaDoc or on the Property Expression Language wiki page

A second constructor on PropertyModel is available for models that wish to access properties involving type conversions:

Code Block

public PropertyModel(final Object modelObject, final String expression, Class propertyType)

...

.PropertyResolver JavaDoc or on the Property Expression Language wiki page

Compound property models

If you have looked at some examples of Wicket already, you may have wondered why components, which generally must have a model, can be constructed with only an id, omitting any model. There are three reasons for this.

...

will convert the contents of the text field to and from an Integer value.

Warning
titleBoundCompoundPropertyModel has been removed in Wicket 6!

In the event that you need more flexibility or property expressions power in your compound models, you can use the BoundCompoundPropertyModel. This class provides three methods you can call on the container's model object to bind a given child Component to a specific property expression and/or type conversion:

Code Block

public Component bind(final Component component, final String propertyExpression)
public Component bind(final Component component, final Class type)
public Component bind(final Component component, final String propertyExpression, final Class type)

...

To

...

see

...

how

...

this

...

might

...

be

...

used,

...

suppose

...

that

...

the

...

stringProperty

...

value

...

needed

...

to

...

be

...

bound

...

to

...

the

...

property

...

expression

...

"person

...

[0

...

].stringProperty"

...

.

...

In

...

the

...

FormInput

...

constructor,

...

we'd

...

say

...

something

...

like

...

this:

Code Block

super(name);
final BoundCompoundPropertyModel formModel = new BoundCompoundPropertyModel(new FormInputModel());
setModel(formModel);
add(formModel.bind(new RequiredTextField("stringProperty"), "person[0].stringProperty"));

The Form is constructed without a model. We then create the Form's model as a BoundCompoundPropertyModel, set it as the Form's model and finally use it to bind the RequiredTextField to the desired property expression.

String Resource Models

Localization of resources in Wicket is supported by the Localizer class. This provides a very convenient way to retrieve and format application and component specific resources according to the Locale that the user's Session is running under. In many cases, it will be sufficient to simply retrieve a localized String in this way, but if a formatted, localized string is desired as a model, a StringResourceModel may be used.

...

where User has a getName() method exposing its "name" property.

The Component class has convenience method which you may find useful:

Code Block
add(new Label("greetings", getString("label.greetings", new Model(user))));

Custom models

If we do not want to use the property expressions/introspection, but instead want to to pull a fresh value from the model on each request in a more strongly typed manner, we could provide our own implementation of IModel:

Code Block
final Person person = new Person();
person.setName("Fritzl");
Model model = new Model()
{
  public Object getObject(wicket.Component component)
  {
    return person.getName();
  }
};
add(new Label("name", model));

...