File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes writing to an Action form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "writing to an Action form" Watch "writing to an Action form" New topic
Author

writing to an Action form

Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
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!!!
Ajay Xavier
Ranch Hand

Joined: Jan 03, 2005
Posts: 109
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

Joined: Jul 18, 2006
Posts: 148
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

Joined: Jul 18, 2006
Posts: 148
does anyone know that solution to my problem?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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


Merrill
Consultant, Sima Solutions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: writing to an Action form