If you're using a CPM there's no need to explicitly set models  for child
components. Also think about what you want to happen on page reload.
Generally I'd imagine you want current data, so set CPM's object on
Page.onBeforeRender():

public class EmployeeMain extends BasePage{

final private Form<Contact> employeeForm;

public EmployeeMain()
{
//super(); //no need - call implied
employeeForm = new Form<Contact>("employeeContactForm", new
CompoundpropertyModel(null)); //we'll set model object @ page render time
add(employeeForm);
employeeForm.add(new TextField<String>("firstName")); //assuming existence
of String contact.getFirstName()  method
employeeForm.add(new TextField<String>("lastName"));
}

@Override
public void onBeforeRender()
{
employeeForm.setDefaultModelObject(getEmployeeSession().getEmployee().getEmpInfo());
//get up-to-date Contact and set as CPM's object
super.onBeforeRender(); //have to call to allow page children to render
themselves
}

that's it. Note that if you set CPM's object in page constructor, then on
page refresh, Contact won't be updated (because constructor won't run, but
onBeforeRender() will).



On Wed, Apr 14, 2010 at 8:39 AM, David Hamilton <
dhamil...@hermitagelighting.com> wrote:

>
> I'm a new Wicket using trying to figure out how to populate a form's
> initial value with data from a bean (that is in the session already).
> I've actually got it working, but I don't think I'm doing the best way.
> public final class EmployeeMain extends BasePage {
>    public EmployeeMain() {
>        super ();
>        Form employeeForm=new Form("employeeContactForm");
>        Contact contact=getEmployeeSession().getEmployee().getEmpInfo();
> employeeForm.setModel(new CompoundPropertyModel(contact));
>        add(new TextField("firstName",new
> Model(contact.getFirstName())));
>        add(new TextField("lastName"));
>
>    }
>
> My issue is with this line:
>    add(new TextField("firstName",new Model(contact.getFirstName())));
> Should I have to set the model even though I'm binding the form to a
> bean has this property?
> What is the correct way to handle setting the initial form value from a
> bean?
>
> Thanks,
>
> David
>
> ********************************************
> Keep it Green! To help protect the environment, please
> only print this email if necessary.
> Printing email can cost more than you think.
> Learn more on our website:
> http://www.hermitagelighting.com/printing_email.php
>
> The information transmitted in this email is
> intended solely for the individual or entity
> to which it is addressed and may contain
> confidential and/or privileged material.
> Any review, retransmission, dissemination or
> other use of or taking action in reliance
> upon this information by persons or entities
> other than the intended recipient is prohibited.
> If you have received this email in error please
> immediately notify us by reply email to the sender.
> You must destroy the original material and its contents from any computer.
> ********************************************
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>

Reply via email to