STRUTS2 Interceptor Detailed Programming Example __struts

Source: Internet
Author: User

Struts2 Interceptor

1. The Interceptor in Struts is a class that implements a class of interceptor interfaces.

2. The interceptor in struts and the filter in the servlet have similar functions, literally, the struts interceptor does something before or after the target is executed, but the same is true of the interceptor in struts, In the specific action before or after the call can do some operations, using a configuration method of management, easy to use. But we are best to learn how to intercept the underlying implementation principles in order to be able to control their own procedures in the course of use. The best way to learn about interceptors is to start by understanding the principle of how interceptors are implemented.

3. The interceptor implements the dynamic proxy model to insert the necessary ancillary services before or after the target executes. In fact, an AOP idea is adopted to reduce system coupling.

Here we introduce a dynamic agent:

Java provides a dynamic proxy implementation pattern: We use an example to implement a dynamic proxy demo:

Involves the agent certainly to have: target object proxy object Interceptor

Relationship between the three: proxy object proxy target object to join the interceptor before or after the target object executes

First we create a target object:

1-1 define an interface first

package Com.snt.struts2.interceptor;

Public Interface Itargetinterface {

Public void dosomething ();

}

1-2 define a target object to implement the target object interface [ target object must implement an interface ]

package Com.snt.struts2.interceptor;

Public class Target implements itargetinterface{

Target object to intercept

Public void dosomething () {

System.out.println ("Do something ...");

}

}

1-3 defines an interceptor (here our interceptor is simpler, is a normal in the class, defines an action to be performed after the target object invokes the work) [two methods defined, representing actions to be performed before and after the target object is invoked]

package Com.snt.struts2.interceptor;

Public class Interceptor {

Public void befor () {

System.out.println ("before");

}

Public void after () {

System.out.println ("after");

}

}

1.4 below to implement the proxy, how to create a proxy object for the target object. Java provides us with a certain agent mechanism.

Java provides a proxy class under the Java.lang.reflect package that can generate a proxy class for a target class:

Use the following simpler approach:

Itargetinterface if = (itargetinterface)

Proxy.newproxyinstance (Foo.class.getClassLoader (),

New class[] {Itargetinterface.class},

handler);

Each parameter is the target class's loader every two parameters: the target implements the interface collection every three parameters: the proxy class's invocation handler object.

A dynamic proxy class (hereinafter referred to as a proxy class) is a class that implements the list of interfaces that are specified at run time when the class is created, and that class has the behavior described below. An agent interface is an interface implemented by a proxy class. A proxy instance is an instance of a proxy class. Each proxy instance has an associated invocation handler object that implements the interface Invocationhandler. Invokes the Invoke method of the calling handler that will be assigned to the instance through a method on the proxy instance of one of the proxy interfaces, passing the proxy instance, identifying the Java.lang.reflect.Method object that calls the method, and an array of type object containing the arguments. The calling handler handles the encoded method call in the appropriate way, and the result it returns is returned as the result of a method call on the proxy instance.

For our example, we also need to create an invocation handler for the proxy class first, as follows:

1.5

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.