Newsletter

Struts2 Custom Interceptor Example, Struts2 Interceptors

Struts » on Apr 4, 2012 { 5 Comments } By Sivateja

Let us see how to create user defined interceptors in struts2, we already know this fact that struts2 by default provided lot of interceptors. In fact we no need to create any custom interceptors 🙂 , but this is the way to create custom interceptors if you would like to.

Let us see the steps to create our own interceptor

  • Create one java class which should implements from Interceptor interface, see all interceptors in struts2 must implements from Interceptor interface only
  • Override all abstract methods in Interface interface, Interceptor contains 3 abstract methods init(), destroy(), intercept(ActionInvocation inv)
  • Finally configure our interceptor class in struts.xml file that’s it, let us see the example

Struts2 Custom Interceptor Example

Required files…

  • index.jsp
  • success.jsp
  • web.xml
  • struts.xml
  • LogingEx.java
  • MyInterceptor.java

Directory Structure

index.jsp

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=verify.action">

success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

Struts 2 Interceptors executed successfully, <br>
you can find the same in console.

LogingEx.java

package java4s;
import com.opensymphony.xwork2.ActionSupport;

public class LogingEx extends ActionSupport{
    private static final long serialVersionUID = 1L;    

    public String execute()
    {
        System.out.println("In action class execute method...!!");

        try
        {
           Thread.sleep(4000);
        } catch(Exception e)
        {

        }        

        return SUCCESS;
    }

}

MyInterceptor.java

package java4s;

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

public class MyInterceptor implements Interceptor{
    private static final long serialVersionUID = 1L;    

    public void init()
    {
        System.out.println("Interceptor init method called.....");
    }

    public void destroy()
    {
        System.out.println("Interceptor destroy method called.....");
    }

    public String intercept(ActionInvocation inv) throws Exception
    {
        System.out.println("This is before invocation.invoke()...");

        String a = inv.invoke();

        System.out.println("This is after invocation.invoke()...");
        // here string a contains "success"
        return a;
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <include file="struts-default.xml"/>
    <package name="a" extends="struts-default">

    <interceptors>    

                 <interceptor name="myOwn" class="java4s.MyInterceptor"></interceptor>

                 <interceptor-stack name="dummyName">
                       <interceptor-ref name="myOwn"/>
                 </interceptor-stack>

    </interceptors>

        <action name="verify" class="java4s.LogingEx">
        <interceptor-ref name="dummyName"/>
        <result name="success">/success.jsp</result>
        </action>

    </package>
</struts>

Output

Note:
In MyInterceptor.java, line number 23 we are calling invoke() method of ActionInvocation. Actually invoke() method will take cares the total journey of executing pre-post logic for our Action class.

 

​ ​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

5 Responses to “Struts2 Custom Interceptor Example, Struts2 Interceptors”
  1. Mohammed Vaseem says:

    Hello java4s,

    Please provide the concepts of ValueStack, ActionContext, OGNL as these are the fundamentals to struts2.

    Thanks
    Mohammed Vaseem

  2. Sri says:

    Hi java4s

    Please provide more Real time points

  3. Pooja Tekwani says:

    Hello Sir,

    I am using Struts2 Jquery Plugin, and if I click on two radio buttons alternatively than two different should be loaded… If you can guide me with my query than it’ll be really helpful to me…

    Thanks & Regards,
    Pooja Tekwani

  4. Hello, Sivateja

    I am continue reader of you post. I read all your post and everything is helpful for me and everyone . I need one example of Struts 2x with display taglib library for pagination. I used Struts 1x and it’s working fine .
    so please provide me any small example as soon as possible which will be helpful for me and another one also.

    Regard,
    Narottam Singh

  5. Yogesh K says:

    Hi,

    I am working on One application and I am calling json action for login functionality. I want to use interceptor to deny multilogin for same user.

    I am trying to add customer inteceptor in Json-default package, but it will throw exception.
    Kindly help me to resolve the same.

    Thanks

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.