PropertyModel
will do what you want already. When a PropertyModel
is queried for its value, it looks in two places:
If a "getter" method exists for the given property, the PropertyModel
calls the getter to retrieve the property's value. Specifically, the PropertyModel
looks for a method named get<Property>
, where <Property>
is the property expression passed to the PropertyModel
constructor, and calls the method using reflection if it exists.
If no "getter" method exists, the PropertyModel
returns the value of the property field directly. Specifically, the PropertyModel
uses reflection find a field that matches the property expression passed to the PropertyModel
constructor. If a matching field is found, the PropertyModel
returns the field's value. Note that the PropertyModel
will check private and protected fields in addition to public fields for a match.
In your case, the property expression used in the PropertyModel
constructor is "password"
, so the PropertyModel
will first look for a method on the user
object called getPassword
. If no such method exists, the PropertyModel
will return the value of the private password
field instead.
Since in your case the PropertyModel
is returning the private field's value instead of calling the "getter", you most likely mistyped the name of the getter in your User
class. For example, f you accidentally typed getPasssword
(with 3 s's), the PropertyModel
won't find it, and will fallback to returning the private field.
EDIT
If you don't like PropertyModel
's default behavior, you can create a subclass of PropertyModel
that will prevent Wicket from trying to read/write to private fields. This way, you can force all property accesses to occur through getters and setters.
I wrote an example BeanPropertyModel
class to demonstrate this:
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.model.PropertyModel;
/**
* A custom implementation of {@link org.apache.wicket.model.PropertyModel}
* that can only be bound to properties that have a public getter or setter method.
*
* @author mspross
*
*/
public class BeanPropertyModel extends PropertyModel {
public BeanPropertyModel(Object modelObject, String expression) {
super(modelObject, expression);
}
@Override
public Object getObject() {
if(getPropertyGetter() == null)
fail("Missing getter");
return super.getObject();
}
@Override
public void setObject(Object modelObject) {
if(getPropertySetter() == null)
fail("Missing setter");
super.setObject(modelObject);
}
private void fail(String message) {
throw new WicketRuntimeException(
String.format("%s. Property expression: '%s', class: '%s'.",
message,
getPropertyExpression(),
getTarget().getClass().getCanonicalName()));
}
}