A small example of the implementation of the STRUTS2 analog interceptor

Source: Internet
Author: User

1. Interceptor interface

 Public Interface Interceptor {    void  Interceptor (actioninvocation invocation);}

2. Make 3 implementations of the interface

 Public classFirstinterceptorImplementsInterceptor { Public voidInterceptor (Actioninvocation invocation) {System.out.println (1);        Invocation.invoke (); System.out.println (-1); }} Public classSecondinterceptorImplementsinterceptor{ Public voidInterceptor (Actioninvocation invocation) {System.out.println (2);        Invocation.invoke (); System.out.println (-2); }} Public classThirdinterceptorImplementsinterceptor{ Public voidInterceptor (Actioninvocation invocation) {System.out.println (3);        Invocation.invoke (); System.out.println (-3); }}

3. An Action object

 Public class Action {    publicvoid  execute () {        System.out.println ("Execute executed!!! ");    }}

4.ActionInvocation initializes the implementation of 3 interceptors to the list, simulating the interceptor stack

1  Public classactioninvocation {2List<interceptor> interceptors =NewArraylist<interceptor>();3     intindex =-1;4Action A =NewAction ();5     6      Publicactioninvocation () {7          This. Interceptors.add (Newfirstinterceptor ());8          This. Interceptors.add (Newsecondinterceptor ());9          This. Interceptors.add (Newthirdinterceptor ());Ten     } One      Public voidInvoke () { Aindex++; -          -         if(Index >= This. Interceptors.size ()) { the A.execute (); -}Else { -              This. Interceptors.get (Index). Interceptor ( This); -         } +     } -}

5. Running

 Public class Main {    publicstaticvoid  main (string[] args) {        new actioninvocation (). Invoke ();    }}

6. Results

1
2
3
Execute executed!!!
-3
-2
-1

A small example of the implementation of the STRUTS2 analog interceptor

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.