StrutsCatalog: Use a base Action for your application

Often, the Actions in your application are able to share a common set of utility methods. To make it easy to share utilities methods, declare a base Action for your application and extend all of your custom Actions from this base. Here's one example of a base Action with several utility methods:

http://cvs.apache.org/viewcvs/jakarta-struts/contrib/scaffold/src/java/org/apache/struts/scaffold/BaseAction.java

Note that to be thread-safe, utility methods must pass all parameters through the method signature. Actions must not share parmeters through member fields.

If key tasks needs to be completed in each Action's  execute  method, one strategy is to create a new abstract methods for your subclasses to use in lieu of  execute . The base Action class does its business, and if all is well, then returns the result of the new method. This allows you to change the signature of your new method, if there is ever any reason for that, and provides a cleaner flow.

{{{ public ActionForward execute(ActionMapping mapping,

where  executeAction  is an abstract method of the base class. We've added an extra parameter here for demonstration purposes. If your application doesn't need to change the signature, then simply retain the Struts execute signature in your own implementation.

-- TedHusted


Comments:

This is a great pattern, particularly if you know that there are certain errors/exceptions that will require common handling. In my company, applications may be available near 24/7 but sections of the database get locked by batch update processes etc. at irregular intervals. As such, I have a base action that checks SQL errors and sends to a global forward dbUpdate when an update is in progress.

--David Hibbs

Comments:

I have been using this idea on mid-size project and finally changed my mind. It's fine but sooner or later you 'll realize that it would be cool to use built-in struts actions like DispatchAction and others that provide useful functionalities. Then, as java does still not provide multiple inheritance, you are forced to either build a full hierarchy of abstract actions (with one abstract actions for each kinda of built-in action you want to reuse), or you end up adding more and more functionality to your base class. It seems to me that the only real interest of this method is when you want to enforce the execution of a given piece of code on each action execution (like verifying that the user is logged in), but infact to do this a proper customization of the ActionServlet or RequestProcessor should be better (or even a Servlet filter but this a more tedious job).

-- Nicolas JOUVE from France

I believe Nicolas is right, this forces people to use inherintance instead of composition. I know theres a big fight between both of them but his problem is something that most programmers will face some day if one keep on using such tatics.

If you use composition there will be no such problem like parallel hierarchy and so on....

So even if your task it suposed to run before each and every action its better to leave it in another class from the beginning than to regreting that in one year because two actions are not supossed to do part of that any longer...

Guilherme Silveira from Brazil

StrutsCatalogBaseAction (last edited 2009-09-20 23:31:23 by localhost)