|
Preface:
Struts2 has been in contact for some time, Student core content by intercepting docking Action, to achieve control jumps View layer. In this paper, according to their own understanding of the Struts2 simulated a Java instance, to facilitate understanding!
Schematic diagram
Through simple diagram above, we can see Struts2 will ServletAPI business process separation allows developers to when a user sends a request to the client better for business process layer by intercepting mechanism, improve development efficiency. Here we have a few Java classes analog Struts2 interceptor implementation.
Interception principle
Code design:
1ActionInvocation
package com.simulink;
import java.util.ArrayList;
import java.util.List;
public class AntionInvocation {
Action a = new Action ();
int index = -1;
List < Interceptor> interceptors = new ArrayList < Interceptor> ();
public AntionInvocation () {
interceptors.add (new FirstInterceptor ());
interceptors.add (new SecondInterceptor ());
}
public void invoke () {
index ++;
// System.out.println ( "index:" + index);
if (index> = this.interceptors.size ()) {
a.execute ();
} Else {
this.interceptors.get (index) .intercept (this);
}
}
}
FirstInterceptor class
package com.simulink;
public class FirstInterceptor implements Interceptor {
@Override
public void intercept (AntionInvocation invocation) {
System.out.println (-1);
invocation.invoke ();
System.out.println (1);
}
}
SecondInterceptor class
package com.simulink;
public class SecondInterceptor implements Interceptor {
@Override
public void intercept (AntionInvocation invocation) {
System.out.println (-2);
invocation.invoke ();
System.out.println (2);
}
}
Class Action
package com.simulink;
public class Action {
public void execute () {
System.out.println ( "execute!");
}
}
5 test class Main
package com.simulink;
public class Main {
/ **
* @param Args
* /
public static void main (String [] args) {
new AntionInvocation () invoke ().;
}
}
Output:
-1
-2
execute!
2
1
Postscript: contact WebWork friends should find struts2 with its very similar, in fact, Struts2 is Struts1 and WebWork combination. Its main technology comes from WebWork! |
|
|
|