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

Action that uses Action

jonathan Greens
Ranch Hand

Joined: Apr 07, 2004
Posts: 139
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

Joined: Feb 15, 2005
Posts: 4864
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.


Merrill
Consultant, Sima Solutions
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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).


A good workman is known by his tools.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Action that uses Action