The WicketModel property is weird?

I am new to Wicket and have tried 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 a password and find out that the default implementation of the PropertyModel is by default tied to a field, not a property (strange name eh?)

add(new PasswordTextField("password", new PropertyModel(user, "password"));

Why would they implement this in the world? And is there an alternative to PropertyModel that defaults to getter and seters?

Thanks?

+5
source share
2 answers

PropertyModelwill do what you want. When a query is PropertyModelrequested for its value, it looks in two places:

  • "getter" , PropertyModel getter . , PropertyModel get<Property>, <Property> - , PropertyModel, , .

  • "getter" , PropertyModel . , PropertyModel , , PropertyModel. , PropertyModel . , PropertyModel .

, PropertyModel, "password", PropertyModel user getPassword. , PropertyModel password.

PropertyModel "getter" , , , user. , f getPasssword ( 3 s), PropertyModel .


PropertyModel , PropertyModel, Wicket / . , getters seters.

BeanPropertyModel, :

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()));
    }
}
+11

spross! :

.

 new Model<String>(){ getObject(){...} setObject(){...}}

, , .

+3

All Articles