Struts2 Learning: Use of interceptor

Keywords: Programming Struts xml Apache JSP

For websites that require login validation, privilege validation and other functions, each action writes a piece of validation code for each request, which is redundant and difficult to maintain.Strts2 provides interceptors to provide a facet, or common component, for these pages for easy maintenance and code reuse.There are many default interceptors for struts, so don't list them all. Start with custom interceptors.

1. Define a custom interceptor:

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class MyInterceptor extends AbstractInterceptor {
    public String intercept(ActionInvocation invocation) throws Exception {
        String output = "Pre-Processing";
        System.out.println(output);
        String result = invocation.invoke();
        output = "Post-Processing";
        System.out.println(output);
        return result;
    }
}

2. Modify struts.xml to declare and use interceptor MyInterceptor in package

<package name="suibian" extends="struts-default">
    <!--Declare custom interceptors-->
    <interceptors>
        <interceptor name="myinterceptor" class="com.owlforest.home.interceptor.MyInterceptor" />
    </interceptors>
    <action name="hello" class="com.owlforest.home.action.HelloWorldAction" method="excute">
        <!--Use custom interceptors-->
        <interceptor-ref name="myinterceptor" />
        <interceptor-ref name="params"/>
        <result name="success">/HelloWorld.jsp</result>
    </action>
</package>

The interceptor works before and after the execution of the com.owlforest.home.action.HelloWorldAction, and for ease of understanding, attach the code for the Action:

import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
    private String name;
    public String excute(){
        System.out.println("excute");
        return "success";
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

3. Executing Procedures

Program implementation refers to Struts2 learning: HelloWorld

After executing the action, you can view the console output and see the following information and instructions in effect.

4. For a package in struts.xml, there may be multiple actions, and multiple actions may use the same set of interceptors. If each action writes the same set of interceptors, it is difficult to maintain.struts2 provides interceptor stacks to help integrate these interceptors for reuse purposes.

For ease of understanding, I wrote two custom interceptors, MyInterceptor and SecInterceptor

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class MyInterceptor extends AbstractInterceptor {
    public String intercept(ActionInvocation invocation) throws Exception {
        String output = "Pre-Processing  Interceptor MyInterceptor";
        System.out.println(output);
        String result = invocation.invoke();
        output = "Post-Processing  Interceptor MyInterceptor";
        System.out.println(output);
        return result;
    }
}
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class SecInterceptor extends AbstractInterceptor {
    public String intercept(ActionInvocation invocation) throws Exception {
        String output = "Pre-Processing  SecInterceptor";
        System.out.println(output);
        String result = invocation.invoke();
        output = "Post-Processing  SecInterceptor";
        System.out.println(output);
        return result;
    }
}

Configure 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>
    <!-- Set up struts Is it development mode, default is false,The test phase is typically set to true. -->
    <constant name="struts.devMode" value="true"/>
    <package name="suibian" extends="struts-default">
        <interceptors>
            <interceptor name="myinterceptor"
                         class="com.owlforest.home.interceptor.MyInterceptor" />
            <interceptor name="secinterceptor"
                         class="com.owlforest.home.interceptor.SecInterceptor" />
            <interceptor-stack name="myinterceptorstack">
                <interceptor-ref name="myinterceptor" />
                <interceptor-ref name="secinterceptor" />
            </interceptor-stack>
        </interceptors>
        <action name="hello" class="com.owlforest.home.action.HelloWorldAction"
                method="excute">
            <interceptor-ref name="myinterceptorstack"/>
            <interceptor-ref name="params"/>
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>
</struts>

5. Because interceptors and interceptor stacks are declared in packages, scopes are only in the currently declared packages. For some validation and exception-related interceptors, they need to be available globally, that is, in all packages. This requires defining a global package so that the corresponding action packages can inherit from the global package.The following:

<?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>
    <!-- Set up struts Is it development mode, default is false,The test phase is typically set to true. -->
    <constant name="struts.devMode" value="true"/>
    <!--Global package-->
    <package name="commominterceptor" extends="struts-default">
        <interceptors>
            <interceptor name="myinterceptor"
                         class="com.owlforest.home.interceptor.MyInterceptor" />
            <interceptor name="secinterceptor"
                         class="com.owlforest.home.interceptor.SecInterceptor" />
            <interceptor-stack name="myinterceptorstack">
                <interceptor-ref name="myinterceptor" />
                <interceptor-ref name="secinterceptor" />
            </interceptor-stack>
        </interceptors>
    </package>
    <!--Inherited from commominterceptor Of package-->
    <package name="suibian" extends="commominterceptor">
        <action name="hello" class="com.owlforest.home.action.HelloWorldAction"
                method="excute">
            <interceptor-ref name="myinterceptorstack"/>
            <interceptor-ref name="params"/>
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>
</struts>

Interceptor stacks and global interceptors work as follows:

Posted by p2003morris on Sat, 08 Jun 2019 10:22:18 -0700