You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Charlesworth, Chico" <Ch...@softwareag.co.uk> on 2002/03/08 11:40:44 UTC

RE: Populating complex java objects in forms - PROBLEM SOLVED, BU T IS THIS A BUG IN STRUTS?

I've found that my problem is that my Customer instance was not initialized
in the Form class, but I wouldn't have thought this should be necessary in
Struts. This then also means that any other complex java objects within
customer would need to be instantiated (i.e. Address) and so on if Address
had other complex java objects.

So it works fine if in the form I define:
Customer customer = new Customer();

Also, if I invoke an Action class and create a new instance of customer in
the Customer form and then forward to the customer jsp, it displays the new
instance values ok, but it still doesn't work when submitting the form if
the Customer instance in the form is not instantiated (customer is null
again).

Chico.

-----Original Message-----
From: Jon Ferguson [mailto:calliopesoft@btinternet.com] 
Sent: 08 March 2002 09:12
To: Struts Users Mailing List
Subject: Re: Populating complex java objects in forms

Do you have the appropriate setXXX() method on the customer for name?
Jon

"Charlesworth, Chico" wrote:

> Hi,
>
> If I've got a complex java object (i.e Customer) in the Form class, I can
> then read off this object in the jsp page, but when submitting to the
Action
> class the Customer object is null.
>
> The jsp would look like:
> <html:form action="/updateCustomer">
>         Customer Name: <html:text property="customer.name"/>
>         <br>
>         <a href="javascript:document.forms[0].submit();">Update
Customer</a>
> </html:form>
>
> So this would display the current customer name, but if I change the
> customer name and then hit the submit link, I find the customer instance
is
> now null in the updateCustomer action class.
>
> Am I doing something wrong, or is it not possible to populate complex java
> objects in the jsp form using struts?
>
> Cheers,
> Chico.
>
> --
> The content of this e-mail is confidential, may contain privileged
material
> and is intended solely for the recipient(s) named above. If you receive
this
> in error, please notify Software AG immediately and delete this e-mail.
>
> Software AG (UK) Limited
> Registered in England & Wales 1310740
> Registered Office: Hudson House, Hudson Way,
> Pride Park, Derby DE24 8HS
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>


-- 
The content of this e-mail is confidential, may contain privileged material
and is intended solely for the recipient(s) named above. If you receive this
in error, please notify Software AG immediately and delete this e-mail.

Software AG (UK) Limited
Registered in England & Wales 1310740
Registered Office: Hudson House, Hudson Way,
Pride Park, Derby DE24 8HS

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Populating complex java objects in forms - PROBLEM SOLVED, BUT IS THIS A BUG IN STRUTS?

Posted by Alex Paransky <al...@individualnetwork.com>.
There is no way in struts to populate the complex JAVA object with data
simply from a submit form.  If you pre-initialize your complex object then
the submit form will be able to populate customer.name,
customer.address.line1, customer.address.zip but ONLY if you have these in
your form as valid properties.

The method setCustomer(Customer c) or Customer getCustomer() will never be
called because there is no such representation of a customer on the page.
Because your form has a scope of request, it goes away after the request is
complete.  So, when you first show the customer on the screen, I presume,
the Customer object is somehow found and populated and put into the form.
At this point, the request is done and over with.  The Customer object that
you have created has been lost.  When the user hits the submit button, a new
request is created.  Since this new request is calling on the action which
requires a form, a NEW form is created with a NEW Customer member.  If you
initialized the customer to a new Customer(), then struts is able to match
properties and put customer.name in to your customer name or
customer.address.city in to the proper field, but it's NOT the same Customer
object (pointer wise) that you have initially found.

So, if you have other properties in the Customer object which are NOT shown
on the page, then they will be reset to what ever they are when you do an
initial new Customer.

If you want to keep the same form, there are two things you can do:

1. Change the scope from request to session.  This will keep the original
form that you created, and since the original customer object that you have
found and populated in to the form.  Using this method, however, will
probably require that your submit action, removes the data from the session.
Otherwise, your session would contain variables which are no longer used
(bad use of resources).

2. Pre-initialize the customer and all the objects inside of the customer
just like you already did.  This will populate the fields on your .jsp form
back into the customer object, but not all the fields of the customer.  Only
those which are directly used on the form.

These are the options I can think of.

Hope this helps.

-------------
-AP_
See my profile at
http://www.myprofiles.com/member/view.do?profileId=128


-----Original Message-----
From: Charlesworth, Chico [mailto:Chico.Charlesworth@softwareag.co.uk]
Sent: Friday, March 08, 2002 2:41 AM
To: 'Struts Users Mailing List'
Subject: RE: Populating complex java objects in forms - PROBLEM SOLVED,
BUT IS THIS A BUG IN STRUTS?



I've found that my problem is that my Customer instance was not initialized
in the Form class, but I wouldn't have thought this should be necessary in
Struts. This then also means that any other complex java objects within
customer would need to be instantiated (i.e. Address) and so on if Address
had other complex java objects.

So it works fine if in the form I define:
Customer customer = new Customer();

Also, if I invoke an Action class and create a new instance of customer in
the Customer form and then forward to the customer jsp, it displays the new
instance values ok, but it still doesn't work when submitting the form if
the Customer instance in the form is not instantiated (customer is null
again).

Chico.

-----Original Message-----
From: Jon Ferguson [mailto:calliopesoft@btinternet.com]
Sent: 08 March 2002 09:12
To: Struts Users Mailing List
Subject: Re: Populating complex java objects in forms

Do you have the appropriate setXXX() method on the customer for name?
Jon

"Charlesworth, Chico" wrote:

> Hi,
>
> If I've got a complex java object (i.e Customer) in the Form class, I can
> then read off this object in the jsp page, but when submitting to the
Action
> class the Customer object is null.
>
> The jsp would look like:
> <html:form action="/updateCustomer">
>         Customer Name: <html:text property="customer.name"/>
>         <br>
>         <a href="javascript:document.forms[0].submit();">Update
Customer</a>
> </html:form>
>
> So this would display the current customer name, but if I change the
> customer name and then hit the submit link, I find the customer instance
is
> now null in the updateCustomer action class.
>
> Am I doing something wrong, or is it not possible to populate complex java
> objects in the jsp form using struts?
>
> Cheers,
> Chico.
>
> --
> The content of this e-mail is confidential, may contain privileged
material
> and is intended solely for the recipient(s) named above. If you receive
this
> in error, please notify Software AG immediately and delete this e-mail.
>
> Software AG (UK) Limited
> Registered in England & Wales 1310740
> Registered Office: Hudson House, Hudson Way,
> Pride Park, Derby DE24 8HS
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
The content of this e-mail is confidential, may contain privileged material
and is intended solely for the recipient(s) named above. If you receive this
in error, please notify Software AG immediately and delete this e-mail.

Software AG (UK) Limited
Registered in England & Wales 1310740
Registered Office: Hudson House, Hudson Way,
Pride Park, Derby DE24 8HS

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>