Struts2 Interceptor Example Project configuration Files

Welcome to Struts 2 Interceptor Example. While working on Struts 2, most of the time you will spend on Action Classes but Interceptors are the backbone of Struts 2 Framework.

Struts 2 Interceptor

Struts 2 interceptors are responsible for most of the processing done by the framework. For example, passing request params to action classes, making Servlet API request, response, session available to Action classes, validation, i18n support etc.

Struts 2 provides a bunch of interceptors and most of them are defined in struts-default package and used in defaultStack interceptor stack. Interceptors are the power of Struts 2 framework that plays a crucial role in achieving high level of separation of concerns.

In our Struts 2 Beginners Tutorial, we looked at the Struts 2 architecture and saw that every request and response passes through interceptors. Hence interceptors can inject different objects, perform specific operations on request and response and do some cross cutting tasks for the application.

ActionInvocation is responsible to incapsulate Action classes and interceptors and to fire them in order. The most important method for use in ActionInvocation is invoke() method that keeps track of the interceptor chain and invokes the next interceptor or action. This is one of the best example of Chain of Responsibility pattern in Java EE frameworks.

Struts 2 API reads the interceptors defined for an action in the order it’s declared. So if interceptor1 and interceptor2 are defined for action1 then ActionInvocation will invoke them in following chain.

interceptor1 -> interceptor2 -> action1 -> interceptor2 -> interceptor1

If any of the interceptor intercept() method decides that action should not get executed, it can return a string value and response will be created from global result with that name from struts configuration file.

Struts 2 Interceptors and Global Results configuration

We can define global results in struts.xml file as:

These results can be used by any of the action classes.

Defining interceptors for a package includes several steps and we need to define them with interceptors, interceptor, interceptor-stack and default-interceptor-ref elements. They should be the first elements of the package or else you will get error message as

The content of element type “package” must match “(result-types?,interceptors?,default-
interceptor-ref?,default-action-ref?,default-class-ref?,global-results?,global-exception-
mappings?,action*)”.

Struts 2 Interceptor Example

A simple example of interceptor configuration is:

Note that defaultStack is already configured in struts-default package, that’s why we don’t need to define that in above example.

Let’s create a simple application where we will use custom interceptor class to provide authentication for our application. Using interceptor will make our application loosely coupled, flexible and configurable. Our final project will look like below image.

Struts2 Interceptor Example Project configuration Files

web.xml

pom.xml

pom.xml and web.xml configuration files are self understood.

struts.xml

Notice that I am declaring an interceptor authentication which is referring to class com.journaldev.struts2.interceptors.AuthenticationInterceptor.

Also notice that authStack is the default interceptor-stack for the package. I am not using this stack for login.jsp because it’s used for login and at that time user will not have session to authenticate.

Result Pages

login.jsp

welcome.jsp

JSP result pages are easy to understand and nothing related to interceptors.

Model Class

User.java

A simple java bean with properties that are used in result pages. We will use this java bean in action class to hold properties.

Custom Interceptor

UserAware.java

An interface similar to Struts 2 API *Aware interface, we will use it in our custom interceptor to inject values in action class. To learn more about Struts 2 API *Aware interface, please read:

How to get Servlet Session, Request, Response, Context Attributes in Struts 2 Action

AuthenticationInterceptor.java

This is our custom interceptor class that is implementing com.opensymphony.xwork2.interceptor.Interceptor interface. init() and destroy() are interceptor lifecycle methods that we can implement to initialize and destroy some resources used in interceptor intercept() method.

intercept() is the method invoked by ActionInvocation class, so this is where our interceptor code goes. We can get session attributes from ActionInvocation reference and use it to make sure session is valid. If we don’t find the attribute, we are returning global result “login” that will redirect user to login page. If session is valid, then we are injecting user into action class. instanceof keyword is used to make sure that action class is implementing UserAware interface. Finally we are calling ActionInvocation invoke() method that will call the next interceptor or action class in the chain.

Action Classes

LoginAction.java

Notice that LoginAction is implementing SessionAware interface to get session attributes and if user is validated, then putting user attribute in session to be used in authentication interceptor for validation. Notice that LoginAction is not implementing UserAware interface.

WelcomeAction.java

Notice that WelcomeAction is implementing UserAware interface and our AuthenticationInterceptor is injecting it to the action class behind the scene. Notice that action class is simple and there is no code for validating the user session or to set the user bean.

From above action classes code, it’s clear that if we use interceptors smartly, we can reduce a lot of code redundancy for common tasks that we need to do for multiple actions.

When we execute above project, we get following response pages.

If you are not logged in and you will try to invoke login.action, our authentication interceptor will forward you to login page and action class will never be invoked.

That’s all for Struts 2 interceptors tutorial, it’s one of the important features of Struts 2 and you should try to use it for reusability.

By admin

Leave a Reply

%d bloggers like this: