"Struts2": Interceptor Implementation Method Filter

Source: Internet
Author: User
Tags abstract

As we all know, when we use STRUTS2 for project development, if we configure an interceptor for an action, the interceptor intercepts all the methods within that action. However, in some cases, we do not need to intercept all the methods, just want to intercept the specified method, at this time we need to use the Struts2 interceptor method filtering features.

In order to implement the feature of the method filtering, STRUTS2 provides a Methodfilterinterceptor class, which is a subclass of the Abstractinterceptor class, if you want to implement the Interceptor Support method filter feature, That will inherit Methodfilterinterceptor.

The Methodfilterinterceptor class overrides the Intercept method of the Abstractinterceptor class, but provides a dointercept abstract method. From this design method, it can be seen that the Methodfilterinterceptor class intercept has implemented the action interception behavior, but the real interception logic needs to be written by the developers themselves, that is, through the callback Dointercept method implementation. It can be seen that if we want to implement our own interception logic, we should override the Dointercept method.

The following is a simple way to filter the sample to show how to implement the method filter, the code is as follows:

//interception method Interceptor, should inherit Methodfilterinterceptor abstract class public class Myfilterinterceptor extends
	Methodfilterinterceptor {//Simple interceptor name private String name;
	The setter method for setting the name of the simple Interceptor public void SetName (String name) {this.name = name; }//Override the Dointercept () method to implement the intercept logic for the action public String dointercept (actioninvocation invocation) throws Exception {//
		Get the intercepted action instance loginaction action = (loginaction) invocation.getaction ();
		The time the print execution started SYSTEM.OUT.PRINTLN (name + "Interceptor Action---------" + "the time to start the login action is:" + new Date ());
		Gets the time to start executing the action long start = System.currenttimemillis ();
		Executes the latter interceptor of the Interceptor, or directly specifies the action's intercepted method String result = Invocation.invoke ();
		The time the print execution ended System.out.println (name + "Interceptor Action---------" + "The time to execute the login action is:" + new Date ());
		Long end = System.currenttimemillis ();
		Print the time taken to execute the action System.out.println (name + "Interceptor Action---------" + "time to execute the action is" + (End-start) + "milliseconds");
	return result; }
}

As can be seen from the above code, the interception logic above is not much different from the interception logic of the ordinary interceptor, but the method of rewriting is different. It is important to note that in order to implement the properties of a method filter, you must inherit the Methodfilterinterceptor abstract class, and override the Dointercept method to define the interception logic for the action.

In the Methodfilterinterceptor method, two methods are added:

Setexcludemethods: Eliminate the need to filter the method, that is, set the method "blacklist", in the method parameters listed in the method will not be intercepted.

Setincludemethods: Set the method to be filtered, that is, the "whitelist" of the method to be set, and the methods listed in the parameters of the method will be intercepted.

Because the Methodfilterinterceptor class contains the two methods above, its subclasses will also get the two methods, so we can specify in the configuration file Struts.xml the methods that are required or do not need to be intercepted.

<package name= "Lee" extends= "Struts-default" > <!--applications that are required to use interceptors are configured under this element-<interceptors> <!--configuration M Ysimple Interceptors--<interceptor name= "mysimple" class= "Org.ljw.app.interceptor.MyFilterInterceptor" > <!--to Interceptors specify parameter values--<param name= "name" > Interceptors for interception methods </param> </interceptor> </interceptors> <act Ion name= "Login" class= "org.ljw.app.action.LoginAction" > <result name= "Error" >/web-inf/content/error.jsp </result> <result>/WEB-INF/content/welcome.jsp</result> <!--configuration system default Interceptor-<interceptor -ref name= "Defaultstack"/> <!--Apply custom Mysimple interceptors--<interceptor-ref name= ' mysimple ' > <!--re-designation  The property value of the Name property--<param name= "name" > after renaming the Interception method Filter Interceptor </param> <!--Specifies that the Execute method does not need to be intercepted--<param Name= "Excludemethods" >execute</param> </interceptor-ref> </action> <action name= "*" > &lt ; Result>/web-inf/content/{1}.jsp</result> </action> </package> 

        The above configuration file uses the Interceptor parameter Excludemethods developed the Execute method without being intercepted, If you need to specify multiple methods that are not intercepted by interceptors, you can separate them with commas. In addition, if the same method name appears in both Excludemethods and Includemethods, that is, the conflict between the two, and finally the Includemethods parameter is specified as the case, that is, the conflict, the default is the chant interception, Of course, I believe we will not have this kind of contradiction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.