Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Can one action class have more than one Form class

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not possible
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic