There are two interceptors set up here, firstInterception, SecondInterception
1 package struts2_inteception; 2 3 public class firstInterception implements Interception{ 4 public void interceptor(ActionInvocaton invocation){ 5 System.out.println("-1"); 6 invocation.invoke(); 7 System.out.println("1"); 8 9 } 10 }
1 package struts2_inteception; 2 3 public class SecondInterception2 implements Interception{ 4 public void interceptor(ActionInvocaton invocation){ 5 System.out.println("-2"); 6 invocation.invoke(); 7 System.out.println("2"); 8 9 } 10 }
Main function Main class
1 package struts2_inteception; 2 3 public class Main { 4 public static void main(String []args){ 5 new ActionInvocaton().invoke(); 6 } 7 8 }
Interceptor interface Interceptor
1 package struts2_inteception; 2 3 public interface Interception { 4 public void interceptor(ActionInvocaton actionInvocaton); 5 }
An Action class that simulates struts2
package struts2_inteception; public class Action { public void execute(){ System.out.println( "execute() method execution" ); } }
an ActionInvocation class,
package struts2_inteception; import java.util.List; import java.util.ArrayList; public class ActionInvocaton { int index=-1; Action action=new Action(); List <Interception> interceptions= new ArrayList<Interception> (); public ActionInvocaton(){ // Call a series of interceptors here this .interceptions.add( new firstInterception()); this .interceptions.add( new SecondInterception2 ( )); } public void invoke(){ index ++ ; if (index>= interceptions.size()){ // Call the action method action.execute(); } else { // Call the things added in the interceptor this .interceptions.get(index).interceptor( this ); } } }
The execution process of the real struts2 interceptor is as follows:
The result of execution is as follows:
-1
-2
Execution of the execute() method
2
1