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

How to access forms in action classes

Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
Hi, I am wondering how to access a form object from an action class that the form is not associated with.

So for example, if Form-A is processed in the executeRender() method of Action-A, and the executeAction() method of Action-B needs to view the values in Form-A, how does Action-B see those values?

Would I have to place Form-A in the session or is there a better way?

I hope I explained this ok.

Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
I thin you need to explain the flow more in detail
sasikiran uvsr

Joined: Mar 20, 2005
Posts: 10

the html form elements are stored in a sub-class of ActionForm that u specify in struts-config.xml

the sub-class of Action that u write uses those elements of the action form to perform business logic.

u can import the action-form class that u hav written specifically for this action class.
then in the execute method of the action class that you write

Method Signature :
public ActionForward execute( ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws, javax.servlet.ServletException

---> YourActionForm afObject = (YourActionForm) form;

then u can use the elements via their respective getter methods.

hope this helps....
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
I am working on a portlet that uses a custom "struts like" framework, so hopefully the differences aren't causing too much confusion.

I have two methods in my Action class:
1. public ForwardConfig executeRender(ActionConfig mapping, RenderRequest request, RenderResponse response) {
2. public ForwardConfig executeAction(ActionConfig mapping, ActionForm form, ActionRequest request, ActionResponse response) throws PortletException {

The first method prepares the form for render. The second method processes the form after it is submitted. Is there an equivalent to these two methods in a regualar Stuts implementation?

So here is a test scenario for what I am trying to do:
Say I have workflow type application with 10 forms (and 10 JSPs). I want to be able to pass the first form to the second form's Action class when the second form's Action is called. Then I want to be able to pass the second form to the third form's Action class when the third form's Action is called. And so on...

Does this explaination help at all?
I agree. Here's the link:
subject: How to access forms in action classes
It's not a secret anymore!