This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

writing to an Action form

 
Nina Anderson
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • 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!!!
 
Ajay Xavier
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does anyone know that solution to my problem?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic