Implementation of struts2 custom interceptor

Source: Internet
Author: User

The interceptor system is an important part of struts2. For the struts2 framework, it can be understood as an empty container. It is precisely a large number of built-in interceptors that have completed most of the operations of the framework.

Because the interceptor of the struts2 framework is dynamically configured (instead of being written to the Framework source code in hard coding mode), all developers can easily expand the struts2 framework, you only need to provide your own interceptor implementation class and configure it in struts. XML file. In fact, developing your own interceptor in struts2 is so simple that developers can easily put the actions that need to be repeated in multiple actions in the interceptor for definition, this provides better code reuse.

Struts2's interceptor system is a design philosophy of AOP (Aspect-Oriented Programming). It allows developers to develop the AOP method in a simple way.

 

The interceptor mechanism representsSoftware reusePrinciples

 

Significance of interceptor:

The Interceptor is an improvement for calling methods. In fact, when an instance is called an interceptor, this is in terms of its behavior; but from the code perspective, the interceptor is a class, and this class also contains methods, this method is a special method, which is automatically executed before the target method is called ".

 

Step 1: Create an interface dog

Public interface dog {

// Dog Information

Publicvoid Info ();

// Run the dog

Publicvoid run ();

}

 

Step 2: Create a class to implement the above interface and implement the Method

Public class dogimpl implements dog {

Publicvoid Info (){

System. Out. println ("I'm a hound ......");

}

Publicvoid run (){

System. Out. println ("I am running fast .....");

}

}

 

Step 3: Create an interceptor (in fact, the interceptor is no different from a common class)

Public class doginterceptor {

Publicvoid log (){

System. out. println ("************************ common method 1 ********** ******************");

}

Publicvoid safe (){

System. out. println ("& generic method 2 &&&&&&& &&&&&&&&&&&&&&&&&&&&");

}

}

 

Step 4: Generate a proxy object based on the target object

Public class myproxyfactory {

 

Publicstatic object getproxy (object target ){

// Create a processing class for the proxy class

Proxyhandlerproxyhandler = new proxyhandler ();

 

// Pass the target object to the proxy object

Proxyhandler. settarget (target );

/**

* Returns a proxy instance of a specified API. This API can assign a method call to a specified handler.

* Loader-class loader that defines the proxy class

* Interfaces-List of interfaces to be implemented by the proxy class

* H-assign a method call Handler

*/

Returnproxy. newproxyinstance (dogimpl. Class. getclassloader (), Target

. Getclass (). getinterfaces (), proxyhandler );

}

}

 

Step 5: Proxy instance call Handler

Public class proxyhandler implementsinvocationhandler {

// Declare the target object to be proxy

Privateobject target;

// Create an interceptor instance

Doginterceptordoginterceptor = new doginterceptor ();

// When the proxy target method is executed, the invoke method is automatically called.

Publicobject invoke (Object proxy, method, object [] ARGs)

Throwsthrowable {

// Declare the return value

Objectresult = NULL;

// Determine whether the method is info based on the name.

If (method. getname (). Equals ("info ")){

// Call general method 1 before this method

Doginterceptor. Log ();

// Call the underlying method represented by this method object for the specified object with the specified parameter

Result = method. Invoke (target, argS );

// Call general method 2 after this method

Doginterceptor. Safe ();

} Else {

// Directly execute the underlying method if it is not the info Method

Result = method. Invoke (target, argS );

}

// Return

Returnresult;

}

// Input the target object

Publicvoid settarget (object target ){

This.tar get = target;

}

}

 

Step 6: Test

Public class dogtest {

Publicstatic void main (string [] ARGs ){

Dog targetdog = new dogimpl ();

Objectproxy = myproxyfactory. getproxy (targetdog );

Dogdog = NULL;

If (proxy instanceof dog ){

Dog = (DOG) proxy;

}

If (dog! = NULL ){

Dog.info ();

Dog. Run ();

}

}

}

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.