views:

895

answers:

2

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?

+6  A: 

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()));
    }
}
Mike Spross
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.
Allain Lalonde
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.
Mike Spross
+3  A: 

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.

Andreas Petersson
+1 - If this is a "one-time" thing, this is the easiest way to get the needed functionality.
Mike Spross