Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Action that uses Action

 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to chain two Actions together?
a lot of times I found that I need to have Action B that needs to do something then do Action C. What is the best practice here?

thanks
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan,

Here is the link to the section of the Struts Newbie FAQ that explains this:

http://struts.apache.org/faqs/newbie.html#chaining

In this link it states that as a rule, chaining of actions is not recommended, and I agree with this. If you have common logic in two action classes, it's better to refactor and put that logic in a utility class that can be called by either action.

However, there are times when chaining does make sense. Example: you have one action that goes to the database, populates an ActionForm bean and displays data, and you have another action that updates the data and then redisplays it. It would make sense in this case, for the update action to simply forward to the display action rather than repeating the logic to populate and dispaly the form.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted Husted has a few good points about chaining Actions. I especially agree with Ted's statements about business logic belonging outside of the Action classes. That is what I consider best practice.

What Merrill mentions as a good scenario for chaining is also known as setup actions. This practice works well with PRG (Post-Redirect-Get).
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Merrill Higginson:

However, there are times when chaining does make sense. Example: you have one action that goes to the database, populates an ActionForm bean and displays data, and you have another action that updates the data and then redisplays it. It would make sense in this case, for the update action to simply forward to the display action rather than repeating the logic to populate and dispaly the form.


IMO, this case still is a candidate for the refactoring that you described earlier. My approach would be to have two helper/utility classes: one to access the data, the other to populate the form for display.

One of the rare instances I have found action chaining to be appropriate is when trying to keep flow control logic out of JSPs. For example, instead of making a link target a JSP expression, you can link to a specific Action which then examines the context and simply chains to an appropriate action.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic