Welcome to Part-5 of 7-Part series where we are discussing different aspects of Struts2 Framework. In the previous article we saw how to integrate Tiles framework with Struts2.
Today we will explorer the world of Interceptors in Struts2. We will see what Interceptors are and how to configure them in a Struts2 based web application.
Struts 2 Interceptors: Basics
Struts2 provides very powerful mechanism of controlling a request using Interceptors. Interceptors are responsible for most of the request processing. They are invoked by the controller before and after invoking action, thus they sits between the controller and action. Interceptors performs tasks such as Logging, Validation, File Upload, Double-submit guard etc.
The request processing lifecycle of Struts2 framework is pretty much discussed Part 1 – Introduction to Struts2 Framework.
Request is generated by user and sent to Servlet container.
Servlet container invokes FilterDispatcher filter which in turn determines appropriate action.
One by one Intercetors are applied before calling the Action. Interceptors performs tasks such as Logging, Validation, File Upload, Double-submit guard etc.
Action is executed and the Result is generated by Action.
The output of Action is rendered in the view (JSP, Velocity, etc) and the result is returned to the user.
Thus the Struts2 Interceptors removes cross cutting tasks such as logging from action components and create cleaner separation of MVC.
Struts2 comes with default list of Interceptors already configured in the application in struts-default.xml file. We can create our own custom Interceptors and plugin into a Struts2 based web application.
Framework creates an object of ActionInvocation that encapsulates the action and all the interceptors configured for that action. Each interceptors are called before the action gets called. Once the action is called and result is generated, each interceptors are again called in reverse order to perform post processing work. Interceptors can alter the workflow of action. It may prevent the execution of action.
Our goal will be to create a customer interceptor MyLoggingInterceptor, which will log the request before any action is called. Also it will prints the Action class name and execution time of action in milliseconds.
Create Logging Interceptor
Create a java class MyLoggingInterceptor in package net.viralpatel.struts2.interceptors and copy following content into it.
This code has to be added after <result-types > tag in <package ></package>
Here we have configured a new interceptor mylogging with tag <interceptor >. Also note that we have defined an interceptor-stack with name loggingStack. This is to make sure Sturts2 calls all the default interceptors as well while calling our custom interceptor. This is very important as the validation logic will not work in our struts2 application if we ignore the default stack of interceptors.
We can make the new loggingStack as default interceptor stack or can configure it at each action level. In order to make it default stack, we should add following in struts.xml
Once we add above code in Struts.xml, the logginStack will be applied to all the actions in that package.
Also we may want to apply the custom interceptor stack to only certain actions. To do so, we must add interceptor-ref tag in action.