File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes DynaActionForm and instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "DynaActionForm and instance variables" Watch "DynaActionForm and instance variables" New topic

DynaActionForm and instance variables

geofrey pony

Joined: Jul 18, 2006
Posts: 15
Got recently introduced to DynaActionForm with the following snippet.

<form-bean name="CustomerForm"
<form-property name="firstName" type="java.lang.String "/>
<form-property name="lastName" type="java.lang.String initial="Doe"/>

I can imagine that DynaActionForm would get loaded and instantiated using Class.forName() at Runtime. I also understand that values could be assigned to an objects variable using Reflection. What I do not understand is, how can we create instance variables using reflection? i.e. How can we bind instance variables to a Class loaded at runtime?

Not sure if this message should go in Struts forum or someother Java forum.

Snippet of code explaining how this is possible would be very helpful.
Thanks in advance.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The properties you create are not actually turned into instance variables. They are put into a Map keyed on the property name. For example, the DynaActionform does not have a getFirstName() or a setFirstName() method. It has a get(String) method and a set(String, String) method. If you want to set the value of the first name field, you do it like this:

dynaForm.set("firstName", "John");

To get the value, you do it like this:

String firstName = (String)dynaform.get("firstname");

Because of this awkwarness in using the getter and setter, I really don't like DynaActionForms, and much prefer writing my own subclasses of ActionForm.

Consultant, Sima Solutions
I agree. Here's the link:
subject: DynaActionForm and instance variables
It's not a secret anymore!