Tuesday, March 1, 2011

Struts 2 Interceptors

In this guide I am going to show you how the struts 2 interceptors work.
Interceptors are new in Struts 2, those are classes that does not maintain a state and are automatically called before and after an action invocation.

Struts 2 has a number of interceptor that are called default on every action call, mapped in a stack, called defaultStack. The main default interceptors are the following:

  • Exception: allows to map a particular exception on a view;
  • Prepare: allows to call a method to initialize an Action if it implements a determined interface;
  • I18n: manage the locale for the current user;
  • Debugging: allows to activate the views debug;
  • FileUpload: allows to manage the file upload;
  • Validation: allows to validate the form fields, based on the xml validation files.
In the next picture I show you the default stack of interceptors:


If we implement a new custom interceptor for our application to be called before every action call as the default interceptors, we could create a new stack for it or put it in the default stack.
If we put it in the default stack, we need to list all the other interceptors defined default by struts, else it will not call those anymore. 
In the next picture I show you how I defined my interceptor in my struts.xml: 



As you can see from the picture above, I defined my new interceptor called loginInterceptor, then I listed the defaultStack with only the interceptors defined by struts 2.
Next, I created a new stack called mainStack on which I put my brand new interceptor and the default stack. 
After that I defined which one is the default stack that struts has to call: 
Here we tell Struts the default stack to call is the mainStack defined by me, in which there is my loginInterceptors and all the default interceptors referenced by the defaultStack.

Now I show you how to create an interceptor.
An interceptor class must extend the AbstractInterceptor abstract class of which we must implement the method  public String intercept(ActionInvocation invocation) throws Exception.
This method, as all the methods of struts actions, returns a String that in this case is the action that we are calling or, if any error occurs, any action defined by us to handle the error occurred.

My example of interceptor controls if the user that is using the application is logged in, looking for the session parameter userName. If the userName parameters is not null and not empty, it returns the action called by the user. If the user is not in session, but it is calling the loginAction to log in, then we return the action called by the user to allow him to log in. If the user is not in session, and the action called by the user is not the LoginAction, then we return a expiredSession result that sends the user back to the login page:


From the imageshowed above you can see that the ActionInvocation object has a lot of useful methods:
  • invocation.getAction().getClass().getName(): Those three methods allows us to retrieve the name of the action class called by the user;
  • invocation.invoke(): This method calls the action method to execute after the interceptor if everything works well inside it. 
You can see in the image the behavior of the interceptor that I expained before it.


3 comments: