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: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())); } }Allain Lalonde : Great answer! Thanks a bunch for clarifying it for me.Allain Lalonde : What I'd been doing was not providing a getter and hoping that is would call the setter. Reason being I didn't want the user object to have a getPassword() method.Mike Spross : It will still call the setter, since you defined one, but when Wicket renders the password field, it will try to "get" the model value, and will end up retrieving the private field. If you want to prevent this, you can override getObject() to return null or a string of "*" characters, or whatever. -
great answer by mike spross! one small addition though:
i'd not use property model in this case. just write
new Model<String>(){ getObject(){...} setObject(){...}}and implement the correct bahavior, which does exactly what you want.
Mike Spross : +1 - If this is a "one-time" thing, this is the easiest way to get the needed functionality.
0 comments:
Post a Comment