aspose file tools*
The moose likes Struts and the fly likes Why do we need ActionForm ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Why do we need ActionForm ?" Watch "Why do we need ActionForm ?" New topic
Author

Why do we need ActionForm ?

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Can we not use html instead ? I know that Action form provides validate and reset methods,however we have reset in html and also validation logic can be provided in the execute method of our action class rather than in ActionForm. So why use ActionForm ?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

If you don't have ActionForms, then where will be the form data that the users fill up go?? You'll have to manually extract all the request data from the request object. ActionForms transfer all the data themselves. Coming to validation, having validation logic in execute method isn't a very good idea. Validation logic should be reusable. Also if you validate everything inside the execute method, you'll have to manually send the request back to the input page if validation fails. I don't like the idea of ActionForms too much, but if you are using Struts 1.x, then you have to use them to get form data automatically...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Thanks for the response.
Here in the below code we are not instantiating the ActionForm (user) , any idea why we CANNOT create instance of ActionForm ? If we do not create instance then how are we calling the methods ?

User userForm = (User)form;
String user = userForm.getUserId();


Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The execute method takes an ActionForm as one of its parameters. You don't have to create it because you already *have* it.
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
User userForm = (User)form;
String user = userForm.getUserId();

Somewhere someone would be creating an instance of "User" as per above code. Is it RequestProcessor ? or ActionServlet ??

Sorry for repeating this question again. Just wanted to get a clear understanding of "whats happening in the background". Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Forms are created by the RequestProcessor. The implementation of the form is determined by the form definition in the struts-config file.
Vinod Tiwari
Ranch Hand

Joined: Feb 06, 2008
Posts: 459
    
    1
Read this http://struts.apache.org/1.x/apidocs/org/apache/struts/action/ActionForm.html


Vinod Tiwari | Twitter
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
David Newton,

Just would like to clarify that its the ActionServlet NOT RequestProcessor that creates the instance of Action and ActionForm (please refer pg 257 of Struts in Action). I happened to read it over the weekend.

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The following code is from the default RequestProcessor, from Struts 1.2.7 (circa mid 2005):The comment where it says "Create the form" is where the ActionForm instance is created (or retrieved from session).

SiA covers Struts 1.0 and 1.1, which have been dead and buried for years. If you're asking for historical information, just note it in your questions, otherwise you're likely to get current, useful information.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do we need ActionForm ?