You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by David Hamilton <dh...@hermitagelighting.com> on 2010/04/14 17:39:20 UTC

Newbie Question about populating form values

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-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Newbie Question about populating form values

Posted by James Carman <jc...@carmanconsulting.com>.
That's one of the key concepts of Wicket models.

On Wed, Apr 14, 2010 at 2:04 PM, Nikita Tovstoles
<ni...@gmail.com> wrote:
> Yeah, guess you could do that too, but then you'd have to wrap one model
> into another (since LDM does not do property matching like CPM) + you've to
> extend LDM.
>
> On Wed, Apr 14, 2010 at 9:24 AM, James Carman
> <jc...@carmanconsulting.com>wrote:
>
>> Why not use a LoadableDetachableModel instead of setting on
>> onBeforeRender()?
>>
>> On Wed, Apr 14, 2010 at 12:10 PM, Nikita Tovstoles
>> <ni...@gmail.com> wrote:
>> > 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 <
>> > dhamilton@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-unsubscribe@wicket.apache.org
>> >> For additional commands, e-mail: users-help@wicket.apache.org
>> >>
>> >>
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

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


Re: Newbie Question about populating form values

Posted by Nikita Tovstoles <ni...@gmail.com>.
Yeah, guess you could do that too, but then you'd have to wrap one model
into another (since LDM does not do property matching like CPM) + you've to
extend LDM.

On Wed, Apr 14, 2010 at 9:24 AM, James Carman
<jc...@carmanconsulting.com>wrote:

> Why not use a LoadableDetachableModel instead of setting on
> onBeforeRender()?
>
> On Wed, Apr 14, 2010 at 12:10 PM, Nikita Tovstoles
> <ni...@gmail.com> wrote:
> > 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 <
> > dhamilton@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-unsubscribe@wicket.apache.org
> >> For additional commands, e-mail: users-help@wicket.apache.org
> >>
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Newbie Question about populating form values

Posted by James Carman <jc...@carmanconsulting.com>.
Why not use a LoadableDetachableModel instead of setting on onBeforeRender()?

On Wed, Apr 14, 2010 at 12:10 PM, Nikita Tovstoles
<ni...@gmail.com> wrote:
> 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 <
> dhamilton@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-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

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


Re: Newbie Question about populating form values

Posted by Nikita Tovstoles <ni...@gmail.com>.
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 <
dhamilton@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-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>