You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jerry Jalenak <Je...@LABONE.com> on 2002/10/24 15:58:06 UTC

[OT - MethodUtils] How to invoke method from DynaActionForm

I know that BeanUtils.populate will populate a JavaBean with values from a
HashMap provided the property name match.  Does anyone know of method that
will go the other direction (i.e. JavaBean to HashMap)?  I have a need to
grab all of the properties from a DynaActionBean and populate a HashMap.  I
can get a Map of all of the properties from the DynaActionForm, generate
'get' method names, and I think I can use MethodUtils.invokeMethod to call
the getter.  The problem is, with DynaActionForms, there really isn't a
'getter' - what I get is a 'NoSuchMethodException'.  Does anyone know how I
can get around this?

TIA,



Jerry Jalenak
Web Publishing
LabOne, Inc.
10101 Renner Blvd.
Lenexa, KS  66219
(913) 577-1496
jerry.jalenak@labone.com


This transmission (and any information attached to it) may be confidential and is intended solely for the use of the individual or entity to which it is addressed. If you are not the intended recipient or the person responsible for delivering the transmission to the intended recipient, be advised that you have received this transmission in error and that any use, dissemination, forwarding, printing, or copying of this information is strictly prohibited. If you have received this transmission in error, please immediately notify LabOne at (800)388-4675.



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


Re: [OT - MethodUtils] How to invoke method from DynaActionForm

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Thu, 24 Oct 2002, Jerry Jalenak wrote:

> Date: Thu, 24 Oct 2002 08:58:06 -0500
> From: Jerry Jalenak <Je...@LABONE.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: "'struts-user@jakarta.apache.org'" <st...@jakarta.apache.org>
> Subject: [OT - MethodUtils] How to invoke method from DynaActionForm
>
> I know that BeanUtils.populate will populate a JavaBean with values from a
> HashMap provided the property name match.  Does anyone know of method that
> will go the other direction (i.e. JavaBean to HashMap)?

You might try PropertyUtils.describe().  It and all the other stuff in
beanutils is documented at:

  http://jakarta.apache.org/commons/beanutils/api/

Note -- for general purpose Map->Bean property copying, I'd suggest using
BeanUtils.copyProperties(), which was recently added, instead of
BeanUtils.populate().  The reason for this is that populate() was
specifically designed to support the way Struts copies properties from an
HTTP request, so it does some pretty funky things on indexed properties --
the copyProperties() method, on the other hand, will behave the way you
expect.

If you don't need type conversions, using PropertyUtils.copyProperties()
instead will run a bunch faster.

>  I have a need to
> grab all of the properties from a DynaActionBean and populate a HashMap.  I
> can get a Map of all of the properties from the DynaActionForm, generate
> 'get' method names, and I think I can use MethodUtils.invokeMethod to call
> the getter.  The problem is, with DynaActionForms, there really isn't a
> 'getter' - what I get is a 'NoSuchMethodException'.  Does anyone know how I
> can get around this?
>

DynaActionForm beans offer you two ways to get property values out:

* Since they implement DynaBean, you can call the get() method:

  String address = (String) dynabean.get("address");

* Nearly all of the BeanUtils and PropertyUtils methods know how to
  deal with either DynaBeans or standard JavaBeans:

  String address =
    (String) PropertyUtils.getSimpleProperty(dynabean, "address");


> TIA,
>

Craig


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