Struts√?¬†2 Interceptor

Struts√?¬†2 Interceptor


Posted in : Java Posted on : November 16, 2011 at 6:00 PM Comments : [ 0 ]

In this section, you will learn how to configure intercept in a Struts 2 application. This section contain a login form example with an customer entry form. We will intercept the invocation of action class using Interceptor.

Struts 2 Interceptor

In this section, you will learn how to configure intercept in a Struts 2 application. This section contain a login form example with an customer entry form. We will intercept the invocation of action class using Interceptor.

In some cases, we need to do some task before and after calling Action . This pre and post processing can be handled by Interceptor. Interceptor can implements the features like double-submit guards, type conversion, object population, validation, file upload, page preparation etc.

When a request is made by a user,  that action is mapped to a correct action  and framework calls that action class to handle the request . But before invocation to this action class, this request is intercepted by the configured Interceptor. After invocation and action execution it is again intercepted by the interceptor.  Due to their property of interception of action invocations, they are known as Interceptor.

Sometimes, an interceptor restrict an action from being fired due to double-submit or failed validation. Interceptor can change the state of the of Action before it executes.

EXAMPLE

This section contain a login form example with an customer entry form. We will intercept the invocation of action class using Interceptor.

The hierarchy of the project is given below :

The jar file used in this example is given below :

We are going to embed the interceptor with the login validation example. We have already discussed about login validation framework . Click here to view this example again. Now we are going to embed the interceptor with this code.

First you need to create a Java interceptor class MyLoggingInterceptor in the package com.devmanuals.interceptor as given below :

package com.devmanuals.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyLoggingInterceptor implements Interceptor{

private static final long serialVersionUID = 1L;

public String intercept(ActionInvocation invocation) throws Exception {

String className = invocation.getAction().getClass().getName();
long startTime = System.currentTimeMillis();
System.out.println("Before calling action: " + className);

String result = invocation.invoke();

long endTime = System.currentTimeMillis();
System.out.println("After calling action: " + className
+ " Time taken: " + (endTime - startTime) + " ms");

return result;
}

public void destroy() {
System.out.println("Destroying MyLoggingInterceptor...");
}

public void init() {
System.out.println("Initializing MyLoggingInterceptor...");
}

}

Configuration of Interceptor in struts.xml

For configuring Interceptor in your application, you need to put the following code in <package></package> tag. Put it after <result-types > tag(If it is there) inside <package></package> tag. Given below the code snippet :

<interceptors>
<interceptor name="mylogging" class="com.devmanuals.interceptor.MyLoggingInterceptor">
</interceptor>
<interceptor-stack name="loggingStack">
<interceptor-ref name="mylogging" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>

In spite of this, you also need to specify which Interceptor should be used for which action. If you want to make the above Interceptor as default Interceptor for each action, you can do this as follows :

<default-interceptor-ref name="loggingStack"></default-interceptor-ref>

This should be put before action tag.

But if you want to configure Interceptor for each action class seperately, you need to put the following line :-

<interceptor-ref name="loggingStack"></interceptor-ref>

inside each action tag as given below :

<action name="login" class="com.devmanuals.LoginAction">
<interceptor-ref name="loggingStack"></interceptor-ref>
<result name="success">LoginSuccess.jsp</result>
<result name="error">Login.jsp</result>
</action>

Now your Interceptor is ready to intercept the actions.

OUTPUT

This login application have two action classes and two actions login and customer is defined in struts.xml for handling actions.

The console output before and after login action :

Before calling action: com.devmanuals.LoginAction

Nov 16, 2011 5:23:56 PM com.opensymphony.xwork2.validator.ActionValidatorManagerFactory <clinit>

INFO: Detected AnnotationActionValidatorManager, initializing it...

After calling action: com.devmanuals.LoginAction Time taken: 266 ms

The console output before and after customer action :

Before calling action: com.devmanuals.CustomerAction

After calling action: com.devmanuals.CustomerAction Time taken: 0 ms

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics