Struts 2 interceptor

Source: Internet
Author: User

 

Struts 2 interceptor

Proxy Mode

Abstract topic: a common interface between a real topic and a proxy topic

Real theme: Implements abstract theme and defines the real objects represented by the proxy role.

Proxy topic: an abstract topic that contains references to a real topic role. A proxy role usually performs some operations before or after a client call is passed to a real topic object, instead of simply returning real objects.

 

Dynamic proxy Mode

1. Create an abstract topic Interface

2. Create a real theme class to implement the abstract theme Interface

3. Create a new processing class to implement the java. Lang. Reflect. invocationhandler Interface

4. Client class, which contains a new real theme object and is passed into the processing class

5. Use the newproxyinstance method of the proxy class that comes with JDK to create a dynamic proxy instance.

 

How interceptor works

Interceptor:When an operation (call a method) is performed, it performs column operations before the operation is executed, and after the operation is completed, it performs a series of operations.

Interception:In fact, it dynamically generates a proxy object, which contains the call to the interceptor method. Therefore, the method of the proxy object is also called, so that the interceptor method can be called dynamically.

Example:

1. Create a New Dao interface because the dynamic proxy can only generate a proxy for instances that implement the interface. Define the corresponding method.

2. Dao interface implementation class to implement all DaO interface methods.

3. interceptor class

4. The processing class implements the java. Lang. Reflect. invocationhandler interface. The daoimpl class and interceptor class methods are called in an appropriate order.

5. Proxy factory, this factoryAccept the target proxy objectCreates a processing class object. The target proxy object is passed into the processing class object. Finally, the object is created using Java. Lang. Reflect. Proxy and the proxy object instance is returned.

6. Client class, call proxy

 

Custom interceptor

The first method to customize the Interceptor:

ImplementationCom. opensymphony. xwork2.interceptor. interceptorInterface

It is particularly important to implement its intercept method. You can define various actions in this method.

Package com. opensymphony. xwork2.interceptor;

 

Import com. opensymphony. xwork2.actioninvocation;

 

Import java. Io. serializable;

 

Public interface interceptor extends serializable {

Void destroy ();

Void Init ();

String intercept (actioninvocation Invocation) throws exception;

}

 

The second custom interceptor method:

InheritanceCom. opensymphony. xwork2.interceptor. abstractinterceptorClass

This class implements the aboveInterceptorInterface.

 

Configure the interceptor after you define it:

Configure interceptor

Add the <interceptors> label to struts. XML, which isSub-tag of <package>:

<Interceptors>

<Interceptor name = "Interceptor name" class = "Interceptor implementation class"> </interceptor>

</Interceptors>

If you want to pass parameters:

<Interceptors>

<Interceptor name = "Interceptor name" class = "Interceptor implementation class">

<Param name = "parameter name"> parameter value </param>

</Interceptor>

</Interceptors>

If you need to configure multiple interceptors, add <interceptor> and combine multiple interceptors to form the interceptor stack.

<Interceptors>

<Interceptor name = "Interceptor name" class = "Interceptor implementation class">

<Param name = "parameter name"> parameter value </param>

</Interceptor>

 

<Interceptor-stack name = "Interceptor stack name">

<Interceptor name = "Interceptor 1" class = "Interceptor implementation class">

<Param name = "parameter name"> parameter value </param>

</Interceptor>

<Interceptor name = "Interceptor 2" class = "Interceptor implementation class"> </interceptor>

</Interceptor-stack>

 

</Interceptors>

 

After the interceptor is configured in the preceding method

Configure action to use the Interceptor: struts. xmlsAdd a sub-tag to the <action> tag:

<Interceptor-ref name = "myinter"> </Interceptor-ref>

 

<Interceptor-ref name = "myinter">

<Param name = "myvalue"> ABCD </param>

</Interceptor-ref>

 

This completes the interceptor'sCreate,ConfigurationAndUse. After completing the preceding steps, you can find that the original input verification function is unavailable. If the data format is incorrect, you can directly jump to the logon Failure page. If the data format is correct, the logon fails. The reason is as follows:

When the action is configured, the package inherits the Struts-default package, and the Struts-default package is configuredDefault interceptor StackDefaultstackThis stack defines a large number of interceptors. In struts 2Type conversion,Input ValidationAndFile UploadAll operations are performed by calling the interceptor of these implicit configurations.

If no interceptor is added to the action in the Struts. xml package, the default interceptor will take effect. Once the action in the package adds an interceptor, the default interceptor is equivalent to not added, so it does not work. In this case, you need to manually add sub-tags in <action> to configure the defaultstack:

<Interceptor-ref name = "defaultstack"> </Interceptor-ref>

So everything is OK!

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.