Struts 2 Interceptors Example

Interceptors are powerful mechanism to control the flow of each request. These are custom and default implementations which can be enforced to work as the callback methods at certain point of time. Struts 2 provides handful of default interceptors behind the scenes. One example is the exception handling at action level. Also we can write our own interceptors and add our own business logic inside the custom interceptors.

1. Action Class

package javabeat.net.struts2;

public class Struts2HelloWorldAction{
	public String execute(){
		return "success";
	}
}

2. Custom Interceptor

package javabeat.net.struts2;

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

public class LoggingInterceptor implements Interceptor{

	@Override
	public void destroy() {
		System.out.println("Destroying logging interceptor...");

	}
	@Override
	public void init() {
		System.out.println("Initializing logging interceptor...");

	}
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String className = invocation.getAction().getClass().getName();
        long startTime = System.currentTimeMillis();
        System.out.println("Before calling action class: " + className);
        String result = invocation.invoke();
        long endTime = System.currentTimeMillis();
        System.out.println("After calling action class: " + className
                + " Time taken: " + (endTime - startTime) + " ms");
		return result;
	}

}

3. Struts.xml

If you look at the interceptor definition, we have used interceptor-stack. It is the collection of interceptors defined in one block. The reason why we are doing is to add the default interceptors to the list, otherwise struts 2 will not invoke the default interceptors. 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<constant name="struts.devMode" value="true" />
	<package name="struts2demo" extends="struts-default">
		<interceptors>
			<interceptor name="logginginterceptor"
				class="javabeat.net.struts2.LoggingInterceptor">
			</interceptor>
			<interceptor-stack name="loggingStack">
				<interceptor-ref name="logginginterceptor" />
				<interceptor-ref name="defaultStack" />
			</interceptor-stack>
		</interceptors>
		<action name="InterceptorDemo" class="javabeat.net.struts2.Struts2HelloWorldAction">
			<interceptor-ref name="loggingStack" />
			<result name="success">Result.jsp</result>
		</action>
	</package>
</struts>

4. Result

If you access the application using URL http://localhost:8080/Struts2App/InterceptorDemo, you would see the following output in the console.

INFO: Choosing bean (struts) for (com.opensymphony.xwork2.util.TextParser)
Initializing logging interceptor...
15 Dec, 2013 5:52:02 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
15 Dec, 2013 5:52:02 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
15 Dec, 2013 5:52:02 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/56  config=null
15 Dec, 2013 5:52:02 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1385 ms
Before calling action class: javabeat.net.struts2.Struts2HelloWorldAction
After calling action class: javabeat.net.struts2.Struts2HelloWorldAction Time taken: 64 ms

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!