Introduction to Java from Introduction to Abandon: Basic Interceptor Grammar for Struts 2

Posted by CaseyC1 on Sat, 08 Jun 2019 23:05:15 +0200

Interceptor, as its name implies, is interception, not robbery!!! (Pictures are from the Internet. If you have copyright or moral problems, please notify the blogger to delete them. Thank you.)

It's like a thief.Not This drop.


Going back to the point, what exactly is the interceptor in struts 2? Let's Baidu···

Er, wrong, let's review how Baidu is our skilled worker to do!!!

Remember, it's retrospect, retrospect, retrospect!!!

Ah, please open my second blog and see the schematic of struts 2. There are a lot of them on Action.

Interceptor, this is the interceptor, struts2 default configuration of many interceptors, specific content, you can open struts 2-core-2.2.1. jar in the struts-default.xml file, to see the configuration information inside.

The role of interceptors is how attributes defined in our Action get the data passed on the page.

And why validate, the data validation method written in the previous article, can be invoked automatically is also the function of interceptors.


Carelessly, I chatted about 50 cents of nonsense again. Okay, get to the point!!!

Next, let's talk about how to customize the interceptor. As for robbery or robbery, it depends on what the officials mean.···

(Again, the pictures are from the Internet. If you have any questions, please contact the blogger.)


There are two kinds of custom interceptors in struts 2, one is robbery, the other is robbery!!!

Ah, it's an interception class (all methods in the class intercept), an interception method!!!

  1. AbstractInterceptor

  2. Method Filter Interceptor


The specific usage is as follows:

AbstractInterceptor

    1.1) Custom class MyInterceptor inherits AbstractInterceptor and implements corresponding methods

public class MyInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        String result;
	System.out.println("Action Intercept before execution!");
	result = invocation.invoke();//That means I'm done with Black Wind Ridge. You can go to the next stop.
	System.out.println("That's it. Go ahead....");
	return result;
    }
}

    1.2) Modify struts.xml, add an interceptor, and use the interceptor to intercept the corresponding Action

<package name="default" namespace="/" extends="struts-default">
    <interceptors>
        <interceptor name="myInterceptor" class="com.pxy.interceptor.MyInterceptor"></interceptor>
    </interceptors>
    <!-- Wildcard mode call -->
    <action name="smng_*" class="com.pxy.action.Hello" method="{1}">
        <result name="{1}">/WEB-INF/jsp/singer_{1}.jsp</result>
	<result name="input">/WEB-INF/jsp/singer_{1}.jsp</result>
	<!-- Binding interceptor -->
	<interceptor-ref name="myInterceptor"></interceptor-ref>
    </action>
</package>

    1.3) Visit http://localhost:8888/strutsDemo/smng_xxx.action to see the results. (The XXX method here is replaced by the CURD method)

Method Filter Interceptor

    Now we only intercept deletet and select methods. The implementation steps are as follows:

    2.1) Modify the custom class MyInterceptor to inherit the MethodFilterInterceptor class and implement the corresponding method

public class MyInterceptor extends MethodFilterInterceptor {
    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
	String result;
	System.out.println("Action Intercept before execution!");
	result = invocation.invoke();
	System.out.println("That's it. Go ahead....");
	return result;
    }
}

    2.2) Modify the struts.xml file and configure the interceptor method to intercept

<!-- Wildcard mode call -->
    <action name="smng_*" class="com.pxy.action.Hello" method="{1}">
	<result name="{1}">/WEB-INF/jsp/singer_{1}.jsp</result>
	<result name="input">/WEB-INF/jsp/singer_{1}.jsp</result>
	<interceptor-ref name="myInterceptor">
		<param name="includeMehtods">delete,select</param>
	</interceptor-ref>
    </action>

    2.3) Visit http://localhost:8888/strutsDemo/smng_xx.action to see the results.

    As you can see from the figure above, the update method is not intercepted by the interceptor.


Okay, let's get to this point about intercepting tolls.

As for what the guest officers want to intercept, they can play by themselves next.···





Topics: Java Struts JSP xml