Struts 2 Note_Interceptor

Keywords: Java Struts JSP

A. What is the interceptor?

Interceptor: Interceptor, which acts as an interceptor for Action.

Filter: A filter that filters requests sent from the client to the server.

Interceptor: Interceptor. Interceptor is the client's access to Action. Finer-grained interception. (Intercepting specific methods in Action). The core functions of Struts 2 framework are implemented by interceptors.

 

Execution process of B. Struts 2

          

 

The client sends an Action request to the server to execute the core filter method. In this method, we call the executeAction() method to call dispatcher.serviceAction() inside the method; create a Action agent inside the method, and finally execute execute() in the Action proxy, and use the invoke method of ActionInvocation in the execute method executed in the proxy. Within this method, a set of interceptors are executed recursively (to complete some functions). If there is no next interceptor, the target action is executed, and page jumps are performed based on the results returned by the action.

 

c. Introduction to Interceptors

1. Building Struts 2 Environment

    

2. Write interceptor class

- Write a class to implement the Interceptor interface or inherit AbstractInterceptor. Of course, it's better to inherit a subclass of AbstractInterceptor, MethodFilterInterceptor, where two variables specifically intercept or release certain methods.

Custom Interceptor:

public class MyInterceptor extends AbstractInterceptor {

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("Custom Interceptor Starts Execution");
        String obj = invocation.invoke();
        System.out.println("Custom Interceptor Execution End");
        return obj;
    }
}

Configure the interceptor:

1. Define interceptor configuration

 

<package name="struts" extends="struts-default"><interceptors>    <!--Configure your own interceptor-->
            <interceptor name="MyInterceptor" class="com.xxx.web.action.MyInterceptor"/>
        </interceptors>
        <action name="upload" class="com.xxx.web.action.FileUploadAction">
            <interceptor-ref name="MyInterceptor"></interceptor-ref>   <!--Introduce the interceptor and make it work-->
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result>/File/success.jsp</result>
        </action>
    </package>

 

2. Custom Interceptor Stack

<interceptors>
            <interceptor name="MyInterceptor" class="com.xxx.web.action.MyInterceptor"/>
            <interceptor-stack name="mystack">              <!--Define your own interceptor stack directly and put all required interceptors in it. action Just put the interceptor stack declaration in effect-->
                <interceptor-ref name="MyInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <action name="upload" class="com.xxx.web.action.FileUploadAction">
            <interceptor-ref name="mystack"></interceptor-ref>
            <result>/File/success.jsp</result>
        </action>

There is a defaultStack in both ways. This is the interceptor stack of the Struts2 framework. As shown in the figure below, there are many interceptors, such as internationalization, validation, file upload and so on. To run struts2, you need the defaultStack interceptor stack. Of course, if you can make it better and more powerful than him, you can use your own, as long as you configure the customization. Interceptor stack, the original struts 2 interceptor will not be effective, to the original effective, to add defaultStack on the line.

 

                        

Posted by dcooper on Wed, 15 May 2019 05:54:19 -0700