This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

DynaActionForm and instance variables

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got recently introduced to DynaActionForm with the following snippet.

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

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.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic