Home Struts Struts Struts2.2.1 Struts2.2.1 Interceptor Example

Ask Questions?

View Latest Questions


 
 

Struts2.2.1 Interceptor Example
Posted on: January 29, 2011 at 12:00 AM
Advertisement
 In this tutorial, We will discuss about Interceptor example and how to implements Interceptor In the Struts 2.2.1 applications.

Struts2.2.1  Interceptor Example

 In this tutorial, We will discuss about Interceptor example and how to implements Interceptor In the Struts 2.2.1 applications. Here, we use a struts configuration file 'struts.xml' for Interceptor Configuration of the application with the Interceptor and Interceptor-ref tags. 

The following Example will shows how to implement the Interceptor in the Struts2.2.1 --

First we create a JSP file named index.jsp as follows.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>Interceptor Example</title>

</head>

<body>

<h1>Interceptor Example</h1>

<hr>

<a href="Interceptor.action">Interceptor Example</a>

</body>

</html>

Here is the Interceptor.jsp 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Interceptors demo example</title>

</head>

<body>

Your welcome, The Process is successful.

</body>

</html>

Here is the  struts.xml  - The Interceptor Configuration is takes place in this file 

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="roseindia" extends="struts-default">

 <interceptors>

                    <interceptor name="logger" class="roseindia.InterceptorAction">

                     </interceptor>

                 <interceptor-stack name="loggerStack"

                               <interceptor-ref name="logger" />

                              <interceptor-ref name="defaultStack" />

                 </interceptor-stack>

</interceptors>

<action name="Interceptor" class="roseindia.InterceptorAction">

                <interceptor-ref name="loggerStack" />

                <result name="success">/Interceptor.jsp</result>

               <result name="error">/index.jsp</result>

</action>

</package>

</struts>

The action class InterceptorAction.java is as follows.

The Interceptor class must Implements com.opensymphony.xwork.interceptor.Interceptor. This class have three methods.

 init()- 
This method is called when interceptor initialize,
destroy()- 
This method is called when Interceptor destruct,
intercept(ActionInvocation invocation)-
This method is called where we place the code to do work.

package roseindia;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

public class InterceptorAction implements Interceptor {

public String intercept(ActionInvocation invocation) throws Exception {

String className = invocation.getAction().getClass().getName();

long startingTime = System.currentTimeMillis();

System.out.println("Before calling action: " + className);

String result = invocation.invoke();

long endingTime = System.currentTimeMillis();

System.out.println("After calling action: " + className

+ " Time taken: " + (endingTime - startingTime) + " ms");

return result;

}

public void destroy() {

System.out.println("Destroying InterceptorAction...");

}

public void init() {

System.out.println("Initializing InterceptorAction...");

}

public String execute() {

return "success";

}

}

This Program produces output on the basis of the Interceptor Action, This  give the output as-

Output:-

The Interceptor Action on the console during the begining to ending of the session  is shown as. the given time is the time consumed by the interceptor when Action (Interceptor.jsp) is run.

Initializing InterceptorAction...
....
....
....
Before calling action: roseindia.InterceptorAction

....
....
....
After calling action: roseindia.InterceptorAction Time taken: 74 ms

...
...
....
Destroying InterceptorAction...

 

index.gif

 

Interceptor.gif

Download Select Source Code

Related Tags for Struts2.2.1 Interceptor Example:


DMCA.com