• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

writing to an Action form

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I'm new to struts and I'm stuck. I'm reading values on a Form from a jsp page and writing to a different Form on another jsp page. However, I can't access the second Form in my ActionForm class, because only the first form was passed over.

So, how can I get access to other Forms in my Action class?

Here's an illustration of what I have:

JSP#1 {

form name = PackageForm

}

JSP#2 {

form name = ServiceForm

}

-------------------------------
###Struts-config.xml##


<form-bean name="ServiceForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="serviceId" type="java.util.Integer" />
</form-bean>

<form-bean name="PackageForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="packageId" type="java.util.Integer" />
</form-bean>

---------------------------------
## Action class ###

DynaActionForm form = (DynaActionForm) form;<-- THIS IS PackageForm
Integer package = form.get("packageId");
....
form.set("serviceId", value); <--- HERE'S THE PROBLEM!!!


From the action class how can I get ServiceForm and write to it???

Please help!!!
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nina,

The execute method of the Action class is provided with an single ActionForm parameter which is the form attribute specified in struts-config.xml for the given Action.

I think you can do something like this,

Create an value object which holds the data received from ActionForm1 i.e PackageForm. Place this value object in the session after calling its corresponding setter methods. In the subsequent action class you can get this value object from the session and populate the ActionForm2 i.e ServiceForm accordingly.

I think this is the best way of doing it.

Regards,
Ajay.
 
Nina Anderson
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response Ajay.

The problem I'm having is that the execute method in the Action class only passes the PackageForm in the request. In the same action class, I'm going to process the data I receive from PackageForm and set the values in ServiceForm.

However, I'm getting a NULLPointer exception, because the Action class is not aware of the ServiceForm. It only got PackageForm as a parameter.

Does struts have a form dispatch method that you can retrieve different Form beans from the Action class?
 
Nina Anderson
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does anyone know that solution to my problem?
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My suggestion would be to stop using DynaActionForm. The problem with DynaActionForm is that you can't instantiate it. It can only be instantiated by the Struts framework. If you write a JavaBean that extends ActionForm and specify that as the type in your <form-bean> stanza, you can then instantiate it and put it in the proper scope. Struts will then use it when the second JSP is displayed. Example:

The ActionForm bean

Code in the execute method of the action class
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic