You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Marco Tedone <m_...@hotmail.com> on 2003/03/22 09:54:09 UTC

Retrieving properties from a DynaValidatorForm

Hi, I'm dealing with DynaValidatorForm. Everything works fine, until I come
in the Action after the validation and I need to retrieve my properties.
With the standard ActionForm I had simply to cast the right ActionForm type
for the parameter 'form' and then access the get/set methods in order to
retrieve the properties value. I didn't find nothing in the
DynaValidatorForm which could aim to get properties value. The alternative
is to use the request.getParameter("..") method, which is what I'm doing at
present.

Does anyone of you have any idea on how to do that, without subclassing the
DynaValidatorForm(which is the reason why I'm using DynaValidatorForm?).

Regards,

Marco

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Retrieving properties from a DynaValidatorForm

Posted by Dan Allen <da...@mojavelinux.com>.
Marco Tedone (m_tedone@hotmail.com) wrote:

> Thank you Dan, your suggestions are inviting...In which cases can you use
> BeanUtils.copyProperties? Sorry for that question, I hadn't time to go
> trough the documentation, so If you can give me some insights, otherwise
> I'll wait until I'll have some more time and will investigate on the
> BeanUtils.copyProperties method.

If you have a form that just populates or updates a bean in your
model (such as a change of address form) you can just retrieve the
model bean (the Address) from the data source, and then use
BeanUtils.copyProperties which will map the getter/setter methods
from your model to your form properties and do the necessary
conversions.  You will have to name your form elements the same name
as your model element names to take advantage of this, but it saves
a lot of editing for when you add a new field.  Image the difference
between

address.setStreet1(form.getAsString("street1"));
address.setStreet2(form.getAsString("street2"));
address.setCity(form.getAsString("city"));
address.setState(form.getAsString("state"));
address.setZipCode(form.getAsString("zipCode"));

and

BeanUtils.copyProperties(address, form);

much simpler for the "common" data.  Then for any fancy stuff (such
as MD5-summing passwords) you go ahead and populate the model bean
using any necessary logic.

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
"I'm old enough to know better, but still too young to care."
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: Retrieving properties from a DynaValidatorForm

Posted by Marco Tedone <m_...@hotmail.com>.
Thank you Dan, your suggestions are inviting...In which cases can you use
BeanUtils.copyProperties? Sorry for that question, I hadn't time to go
trough the documentation, so If you can give me some insights, otherwise
I'll wait until I'll have some more time and will investigate on the
BeanUtils.copyProperties method.

Regards,

Marco

> -----Original Message-----
> From: Dan Allen [mailto:dan@mojavelinux.com] 
> Sent: Saturday, March 22, 2003 4:03 PM
> To: Struts Users Mailing List
> Subject: Re: Retrieving properties from a DynaValidatorForm
> 
> 
> > The alternative is to use the request.getParameter("..") 
> method, which 
> > is what I'm doing at present.
> 
> You definitly don't want to be doing this.  First cast your 
> form to a DynaValidatorForm and then case each property as 
> appropriate:
> 
> String name = (String) form.get("name");
> 
> ...however,
> 
> I have found that sometimes I do a lot of work to convert.  
> So I write a base form class that extends DynaValidatorForm 
> with methods such as
> 
> getAsDouble
> getAsString
> getAsInteger
> getAsFloat
> getAsBoolean
> 
> and so an and so forth.  Those methods just use ConvertUtils 
> to convert from String to whatever I need.  But that is just 
> one solution.  In most cases you don't even need this if you 
> are using BeanUtils.copyProperties
> 
> Dan
> 
> -- 
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> Daniel Allen, <da...@mojavelinux.com>
> http://www.mojavelinux.com/
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> Real programmers just hate to get up in the morning, and 
> contrary to Ordinary People, they're in better shape as 
> it gets closer to nighttime.
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Retrieving properties from a DynaValidatorForm

Posted by Dan Allen <da...@mojavelinux.com>.
> The alternative is to use the request.getParameter("..") method,
> which is what I'm doing at present.

You definitly don't want to be doing this.  First cast your form to
a DynaValidatorForm and then case each property as appropriate:

String name = (String) form.get("name");

...however,

I have found that sometimes I do a lot of work to convert.  So I
write a base form class that extends DynaValidatorForm with methods
such as

getAsDouble
getAsString
getAsInteger
getAsFloat
getAsBoolean

and so an and so forth.  Those methods just use ConvertUtils to
convert from String to whatever I need.  But that is just one
solution.  In most cases you don't even need this if you are using
BeanUtils.copyProperties

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Real programmers just hate to get up in the morning, and 
contrary to Ordinary People, they're in better shape as 
it gets closer to nighttime.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: Retrieving properties from a DynaValidatorForm

Posted by Marco Tedone <m_...@hotmail.com>.
Thank you Robert, I tried and it works.

Marco

> -----Original Message-----
> From: Robert Taylor [mailto:rtaylor@mulework.com] 
> Sent: Saturday, March 22, 2003 11:19 AM
> To: Struts Users Mailing List
> Subject: RE: Retrieving properties from a DynaValidatorForm
> 
> 
> Marco, you should be able to use the get().
> 
> String name = (String)form.get("name");
> 
> robert
> 
> > -----Original Message-----
> > From: Marco Tedone [mailto:m_tedone@hotmail.com]
> > Sent: Saturday, March 22, 2003 3:54 AM
> > To: Struts-user-list
> > Subject: Retrieving properties from a DynaValidatorForm
> >
> >
> > Hi, I'm dealing with DynaValidatorForm. Everything works 
> fine, until I 
> > come in the Action after the validation and I need to retrieve my 
> > properties. With the standard ActionForm I had simply to cast the 
> > right ActionForm type
> > for the parameter 'form' and then access the get/set 
> methods in order to
> > retrieve the properties value. I didn't find nothing in the
> > DynaValidatorForm which could aim to get properties value. 
> The alternative
> > is to use the request.getParameter("..") method, which is what
> > I'm doing at
> > present.
> >
> > Does anyone of you have any idea on how to do that, without 
> > subclassing the DynaValidatorForm(which is the reason why I'm using 
> > DynaValidatorForm?).
> >
> > Regards,
> >
> > Marco
> >
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: Retrieving properties from a DynaValidatorForm

Posted by Robert Taylor <rt...@mulework.com>.
Marco, you should be able to use the get().

String name = (String)form.get("name");

robert

> -----Original Message-----
> From: Marco Tedone [mailto:m_tedone@hotmail.com]
> Sent: Saturday, March 22, 2003 3:54 AM
> To: Struts-user-list
> Subject: Retrieving properties from a DynaValidatorForm
>
>
> Hi, I'm dealing with DynaValidatorForm. Everything works fine,
> until I come
> in the Action after the validation and I need to retrieve my properties.
> With the standard ActionForm I had simply to cast the right
> ActionForm type
> for the parameter 'form' and then access the get/set methods in order to
> retrieve the properties value. I didn't find nothing in the
> DynaValidatorForm which could aim to get properties value. The alternative
> is to use the request.getParameter("..") method, which is what
> I'm doing at
> present.
>
> Does anyone of you have any idea on how to do that, without
> subclassing the
> DynaValidatorForm(which is the reason why I'm using DynaValidatorForm?).
>
> Regards,
>
> Marco
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org