interceptor stack in Struts2

interceptor stack in Struts2

An interceptor stack in Struts2 is a collection of interceptors grouped together. When an action is executed, the interceptors in the stack are invoked in the order they are defined. This provides a way to apply multiple interceptors to an action in a systematic and reusable manner. Interceptor stacks help manage cross-cutting concerns such as logging, validation, and authentication by separating them from business logic.

interceptor stack in Struts2

Benefits of Using Interceptor Stacks:

  • 1.  Modularity : Allows for the modular application of common functionalities across multiple actions.
  • 2.  Reusability : Interceptor stacks can be reused across different actions or packages.
  • 3.  Order of Execution : Defines a clear order of execution for the interceptors.

Java Example

Let’s create an interceptor stack that includes a custom logging interceptor and a built-in validation interceptor.

Step 1: Create the Custom Interceptor
java
package com.example.interceptor;

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

public class LoggingInterceptor extends AbstractInterceptor {

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("Before action execution");
        String result = invocation.invoke();
        System.out.println("After action execution");
        return result;
    }
}

Step 2: Configure the Custom Interceptor
xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">
        
        <!-- Define the custom logging interceptor -->
        <interceptors>
            <interceptor name="logging" class="com.example.interceptor.LoggingInterceptor"/>
            
            <!-- Define an interceptor stack -->
            <interceptor-stack name="customStack">
                <interceptor-ref name="logging"/>
                <interceptor-ref name="validation"/>
            </interceptor-stack>
        </interceptors>
        
        <!-- Apply the interceptor stack to an action -->
        <action name="hello" class="com.example.action.HelloAction">
            <interceptor-ref name="customStack"/>
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>

Step 3: Create Struts2 Action
java
package com.example.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {
    
    private String message;
    private String name;

    @Override
    public String execute() {
        if (name == null || name.isEmpty()) {
            addFieldError("name", "Name is required.");
            return INPUT;
        }
        message = "Hello, " + name + "!";
        return SUCCESS;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Step 4: Create JSP Page
jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
    <title>Hello</title>
</head>
<body>
    <h1><s:property value="message" /></h1>
</body>
</html>


 helloForm.jsp 

jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
    <title>Hello Form</title>
</head>
<body>
    <s:form action="hello">
        <s:textfield name="name" label="Name"/>
        <s:fielderror fieldName="name"/>
        <s:submit value="Submit"/>
    </s:form>
</body>
</html>

Explanation of the Example

  • 1.  Custom Interceptor : LoggingInterceptor.java logs messages before and after action execution.
  • 2.  Interceptor Stack Configuration : The struts.xml file defines an interceptor stack named customStack which includes the logging interceptor and the built-in validation interceptor.
  • 3.  Struts2 Action : HelloAction.java checks if the name field is empty and adds a field error if necessary. It sets a message based on the name.
  • 4.  JSP Pages :
    • hello.jsp  displays the greeting message.
    • helloForm.jsp  provides a form for inputting the name and displays field errors if any.

This example demonstrates how to create and configure an interceptor stack in Struts2, showing how to apply multiple interceptors to an action.