Using Action inheritance to enhance Struts applications


Previously, the web application I work with had all Action classes inherit from the Strut’s Action class.  We noticed that on each request, all our actions had to do some user authentication and error catching and EJB session facade creating.  Many new actions that were created neglected proper checking.  We decided to create our own base abstract action classes for our action classes to inherit from.  The base abstract action classes inherit from the Strut’s Action class and implement the ‘execute’ method.  Within the execute method we put the authentication checking code and set variables to hold the request, response, and frequently used session facades.  The execute method also declares an abstract method called either getJSON or getMapping to perform the action and return the forwarding location to the view/jsp or else a JSON object to be returned to an AJAX call.

We implemented the new abstract base classes and changed all our action classes to inherit from them.  Not only did we enhance the security and consistency of the system, but we reduced the code base by hundreds of lines of code.

Note: Only one instance of each Action class is generated by Struts, so you cannot use member variables in these classes.  All variables setup need to be local and passed to the getJSON and getMapping methods via parameters.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s