Struts 2 Custom Interceptor Example Using @InterceptorRef Annotation

By Arvind Rai, May 11, 2014
Struts 2 supports strong custom interceptor annotation and xml based. In this example we will use @InterceptorRef to support custom interceptor. Custom interceptor is a class which implements com.opensymphony.xwork2.interceptor.Interceptor. That contains three methods init, intercept and destroy. We need to configure interceptors tag in struts 2 xml where our custom interceptor class will be configuired. To support custom interceptor in our struts 2 application, we need do below steps.

Create a Class Implementing Interceptor Interface

In our example I have created MyCustomInterceptor which implements com.opensymphony.xwork2.interceptor.Interceptor interface.
MyCustomInterceptor.java
package com.concretepage.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MyCustomInterceptor implements Interceptor {
	private static final long serialVersionUID = 1L;
	public void destroy() {
		System.out.println("MyCustomInterceptor: Inside destroy");
	}
	public void init() {
		System.out.println("MyCustomInterceptor: Inside init");
	}
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		System.out.println("MyCustomInterceptor: Inside intercept");
		return actionInvocation.invoke();
	}
} 
The methods defined in Interceptor Interface have been described below.
init() : init() method is called when server starts.
intercept(ActionInvocation args): This method is called when the intercepting action class is called. intercept() method runs before executing action class.
destroy(): destroy() method is called when application is un deployed or application is stopped. The purpose of this method is for cleanup.

Configure <interceptors> Tag in struts.xml

As we have created our custom interceptor that is MyCustomInterceptor. Now we need to let struts know about it. All we have to do is add it into interceptor-stack as below.
struts.xml
<struts>
    <package name="default" namespace="/" extends="struts-default">
        <interceptors>   
            <interceptor name="myCustomInterceptor" class="com.concretepage.interceptor.MyCustomInterceptor" />
            <interceptor-stack name="customStack">
                <interceptor-ref name="myCustomInterceptor"/>
            </interceptor-stack>
        </interceptors>      
    </package>
    <constant name="struts.devMode" value="true" />
</struts> 

Use @InterceptorRef Annotation in Action Class

Now the time is to configure @InterceptorRef in our action class. We need to refer interceptor-stack name in @InterceptorRef at class level.
ResultAction.java
package com.concretepage.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/user")
@ResultPath(value="/")
@InterceptorRef(value="customStack")
@ParentPackage(value ="default")
public class ResultAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private String name;
	private String email;
	private int age;
	@Action(value="result", results={@Result(name="success",location="result.jsp")})
	public String execute() {
	    System.out.println("ResultAction: Inside execute.");
	    return SUCCESS; 
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
} 
Output
Download source code and run the application using URL http://localhost:8080/Struts2Demo-1/user/user. Output will be as below.
MyCustomInterceptor: Inside init
------------------
------------------
MyCustomInterceptor: Inside intercept
ResultAction: Inside execute.

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us