• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do we need ActionForm ?

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forms are created by the RequestProcessor. The implementation of the form is determined by the form definition in the struts-config file.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic