Struts2 interceptor and struts2 interceptor

Source: Internet
Author: User

Struts2 interceptor and struts2 interceptor

1. Why use interceptor?

2. interceptor Principle
It has three phases: 1. processing before executing the Action 2. call the invoke () method of ActionInvocation to forward the Controller to the interceptor or return the result string to terminate the execution. 3. processing after the Action is executed


3. Struts2 built-in interceptor
1) params interceptor
2) staticParams interceptor
3) servletConfig interceptor
4) fileUpload interceptor
5) validation interceptor
6) workflow interceptor
7) exceiption interceptor
And so on


Struts2's default interceptor allows you to view struts-default.xml files.

<Interceptors>
<! -- Define interceptor -->
<Interceptor name = "deprecation" class = "org. apache. struts2.interceptor. DeprecationInterceptor"/>

<! -- Define the interceptor Stack -->
<Interceptor-stack name = "basicStack">

<! -- Specify the referenced interceptor -->
<Interceptor-ref name = "exception"/>
<Interceptor-ref name = "servletConfig"/>
<Interceptor-ref name = "prepare"/>
</Interceptor-stack>
</Interceptors>

<! -- Define the default interceptor reference -->
<Default-interceptor-ref name = "defaultStack"/>


Tip: the interceptor Stack has multiple <interceptor-ref>, who is in the front and who takes effect first
Sequence in which the interceptor works: <action> first, then the package, and then the parent package

 

4. Configure the interceptor [custom]
<Interceptor.../>
<Interceptor-ref...>


5. Custom interceptor
Method 1: implement the Interceptor Interface
This interface provides three methods: 1) void init (). After the interceptor is initialized, the system calls back this method before the interceptor is blocked. 2) void destroy (), before the interceptor instance is destroyed. 3) String intercept (ActionInvocation ai) throws Exception: the interceptor action to be implemented.
Tip: ActionInvocatio indicates the status of the current action.

Method 2: Inherit the AbstractInterceptor abstract class [this method is recommended]
This class provides empty implementations of the init () and destroy () methods. You only need to implement the intercept () method.

Method 3: Inherit the MethodFilterInterceptor class: Specifies the method to intercept; specify the method not to intercept

 

--------------

Step: 1) inherit AbstractInterceptor to implement interceptor class
2) define interceptor in struts. xml
<Interceptor name = "..." class = "..."/>
3) <action> reference interceptor
Independent reference: Reference custom and default interceptor
Interception stack reference: defines the interceptor stack to contain custom and default interceptors.


6. interceptor and filter
Similarity: similar functions; similar principles; similar methods;
Difference: different content. The Interceptor is used to intercept Action requests, and the filter can filter almost all content.
The interceptor can obtain the current execution status of the Action, but the filter cannot
Filters are defined in Servlet and configured in Web. xml. interceptors are defined in struts/webWork and configured in struts. xml.

Related Article

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.