Can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Struts2 Custom Interceptor Example, Struts2 Interceptors

Struts » On Apr 4, 2012 | { 2 Comments }

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.

 

 

What you are thinkig....

2 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

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Servlets, Jsps
is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2013 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by str-Graphics.com