I’m new to Wicket and was trying the following configuration:
class User { private String password; ... public void setPassword(String password) { this.password = MD5.encode(password); } ... }
After trying to use the following to bind to the password and finding out that the default implementation of PropertyModel is by default to bound to the field, not the property (weird name eh?)
add(new PasswordTextField('password', new PropertyModel(user, 'password'));
Why in the world would they have implemented it this way? And is there a PropertyModel alternative that uses getter and setters by default?
Thank you?
PropertyModelwill do what you want already. When aPropertyModelis queried for its value, it looks in two places:If a ‘getter’ method exists for the given property, the
PropertyModelcalls the getter to retrieve the property’s value. Specifically, thePropertyModellooks for a method namedget<Property>, where<Property>is the property expression passed to thePropertyModelconstructor, and calls the method using reflection if it exists.If no ‘getter’ method exists, the
PropertyModelreturns the value of the property field directly. Specifically, thePropertyModeluses reflection find a field that matches the property expression passed to thePropertyModelconstructor. If a matching field is found, thePropertyModelreturns the field’s value. Note that thePropertyModelwill check private and protected fields in addition to public fields for a match.In your case, the property expression used in the
PropertyModelconstructor is'password', so thePropertyModelwill first look for a method on theuserobject calledgetPassword. If no such method exists, thePropertyModelwill return the value of the privatepasswordfield instead.Since in your case the
PropertyModelis returning the private field’s value instead of calling the ‘getter’, you most likely mistyped the name of the getter in yourUserclass. For example, f you accidentally typedgetPasssword(with 3 s’s), thePropertyModelwon’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 ofPropertyModelthat 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
BeanPropertyModelclass to demonstrate this: