You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Christian Helmbold <ch...@yahoo.de> on 2009/03/12 12:50:16 UTC

Why to set a model to a form?

Hello,

I wonder why I should set a model to a form, if every form field just holds a reference to the (same) model. Take a look at an example from Wicket in Action (page 91):

public class MyForm extends Form {
  public MyForm(String id) {
    super(id);
    Customer customer = new Customer();
    setModel(new Model(customer)); // Why?
    add(new TextField("name", new PropertyModel(customer, "name")));
    add(new TextField("street", 
        new PropertyModel(customer, "address.street")));
  }
  protected void onSubmit() {
    Customer customer = (Customer)getModelObject();
    String street = customer.getAddress().getStreet();
    // do something with the value oft the street property
  }
}

Is there an advantage over keeping a reference to customer in MyForm like in the following example?

public class MyForm extends Form {
    private Customer customer = new Customer();;
  public MyForm(String id) {
    super(id);
    //setModel(new Model(customer)); Not needed anymore.
    add(new TextField("name", new PropertyModel(customer, "name")));
    add(new TextField("street", 
        new PropertyModel(customer, "address.street")));
  }
  protected void onSubmit() {
    //Customer customer = (Customer)getModelObject(); Not needed anymore.
    String street = customer.getAddress().getStreet();
  }
}

Thanks for enlightenment,
Christian


      


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Why to set a model to a form?

Posted by Jeremy Thomerson <je...@wickettraining.com>.
A model decouples your form from where the object comes from.  For instance,
any time I create a form for adding an entity, I almost always use the same
form for editing the entity.  The form simply takes a IModel<MyEntity> and
doesn't care if that is new Model<Customer>(new Customer()) or new
DBObjectModel<Customer>(Customer.class, 1234)

--
Jeremy Thomerson
http://www.wickettraining.com



On Thu, Mar 12, 2009 at 6:50 AM, Christian Helmbold <
christian.helmbold@yahoo.de> wrote:

>
> Hello,
>
> I wonder why I should set a model to a form, if every form field just holds
> a reference to the (same) model. Take a look at an example from Wicket in
> Action (page 91):
>
> public class MyForm extends Form {
>  public MyForm(String id) {
>    super(id);
>    Customer customer = new Customer();
>    setModel(new Model(customer)); // Why?
>    add(new TextField("name", new PropertyModel(customer, "name")));
>    add(new TextField("street",
>        new PropertyModel(customer, "address.street")));
>  }
>  protected void onSubmit() {
>    Customer customer = (Customer)getModelObject();
>    String street = customer.getAddress().getStreet();
>    // do something with the value oft the street property
>  }
> }
>
> Is there an advantage over keeping a reference to customer in MyForm like
> in the following example?
>
> public class MyForm extends Form {
>    private Customer customer = new Customer();;
>  public MyForm(String id) {
>    super(id);
>    //setModel(new Model(customer)); Not needed anymore.
>    add(new TextField("name", new PropertyModel(customer, "name")));
>    add(new TextField("street",
>        new PropertyModel(customer, "address.street")));
>  }
>  protected void onSubmit() {
>    //Customer customer = (Customer)getModelObject(); Not needed anymore.
>    String street = customer.getAddress().getStreet();
>  }
> }
>
> Thanks for enlightenment,
> Christian
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>