This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can one action class have more than one Form class

 
Dee Gee
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Is it possible to have one action class for more that one form class.
If yes, are there any constraints or any pitfalls we should take care of?

Thank You
DG
 
Manoj Raghuwanshi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not possible
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, yes, this is possible. There is a one-to-one relationship between an Action Mapping (The <action> tag in struts-config.xml) and an ActionForm. However, Multiple Action Mappings can point to the same Action Class, thereby making the same Action class use multiple action forms.

Since the parameter passed into the execute() method is of type ActionForm, you can cast it to whatever subclass of ActionForm you're expecting.

I wouldn't call this best practice, though. Generally it's better to have one action class only deal with one ActionForm.

I can think of one scenario, though, where this would be a reasonable solution: Suppose you had a number of pages that are only slightly different, and the processing for those pages is much the same. You might have all the ActionForms for these pages subclass a base ActionForm that has all the common elements between them. You could also point the Action mappings for each to the same Action class. In the execute method, you could first cast the ActionForm parameter to the base ActionForm class and perform all the common processing, and then based on the action path (mapping.getPath) use if/else logic to cast to the correct subclass and do the remainder of the processing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic