Since struts1.1 was used in the previous project, it has never been used for open-source frameworks.
Struts2 is no different from webwork at the beginning. I want to take a look at webwork. webwork is based on xwork, and xwork exists independently of webiner iner, start with xwork.
Xwork is built in the command mode.
For more information about the command mode, see.
Http://www.dofactory.com/Patterns/PatternCommand.aspx
Several Classes in command mode in xwork
Command Sender: actionproxy
Command transmitter: actioninvaction
Command executor: Action
The class diagram is as follows:
Of course, the internal calls of xwork are not that simple. You can use the xml configuration file to contact each class.
Now we use a program to simulate the following xwork comman mode.
FirstActionproxy
Actionproxy. Java
Public InterfaceActionproxy {
PublicString execute ()ThrowsException;
}
Actionproxy is a command, that is, the client does not directly let the action be executed, but let the actionproxy execute it as a proxy (although it looks a bit like the proxy mode, but not ).
The following is the implementation of actionproxy:
PackageJP. co. wqf;
Public ClassActionproxyimplImplementsActionproxy {
Invocation invocation;
PublicActionproxyimpl (Invocation ){
This. Invocation = invocation;
}
PublicString execute ()ThrowsException {
ReturnInvocation. Invoke ();
}
}
You need to inject an invocation to send commands.
Next, actioninvocation
Actioninvocation. Java
Public InterfaceInvocation {
PublicString invoke ()ThrowsException;
}
Define a call method.
Its implementation is
ImportJava. util. arraylist;
ImportJava. util. iterator;
ImportJava. util. Map;
Public ClassInvocationimplImplementsInvocation {
Iterator interceptors;
Action action;
Map resultmap;
BooleanExecuted =False;
String resultcode;
PublicInvocationimpl (arraylist interceptors, Action action, map resultmap ){
This. Interceptors = interceptors. iterator ();
This. Action = action;
This. Resultmap = resultmap;
}
PublicString invoke ()ThrowsException {
If(Executed ){
Throw NewException ("action has been Execute! ");
}
If(Interceptors. hasnext ()){
Interceptor interceptor = (interceptor) interceptors. Next ();
Interceptor. Intercept (invocationimpl.This);
}Else{
Resultcode = action.exe ctute ();
}
If(! Executed ){
Result result = (result) resultmap. Get (resultcode );
If(Result! =Null)
Result.exe cute ();
Executed =True;
}
ReturnResultcode;
}
}
This is a major class. Many of the processes are transferred here. First, it passes throughInjectionAndRecursive callTo implement method interception, and then call Action to execute real processing, and call different result processing classes (results) according to the processing results)
FinallyActionInterface
Action. Java
Public InterfaceAction {
PublicString exectute ();
}
The action interface defines an execute method to execute an operation. For example, each CRUD operation of a database uses a string returned value to represent the execution result, for example, "success ", "error. In fact, xwork supports any execution method, not just execute. You only need to specify the method to be executed in the xwork. xml file.
The following is the implementation class of the Action interface. An "error" string is returned here.
Public ClassActionimplImplementsAction {
PublicString exectute (){
System.Out. Println ("Action execute ");
// Return "success ";
Return"Error ";
}
}
In addition, there are interceptor and result interfaces, implementation classes.
Interceptor. Java
Public InterfaceInterceptor {
Public VoidIntercept (Invocation)ThrowsException;
}
Interceptorimpl. Java
Public ClassInterceptorimplImplementsInterceptor {
PrivateString name;
PublicInterceptorimpl (string name ){
This. Name = Name;
}
Public VoidIntercept (Invocation)ThrowsException {
System.Out. Println (name + ", start ");
Invocation. Invoke ();
System.Out. Println (name + ", end ");
}
}
Result. Java
Public InterfaceResult {
Public VoidExecute ();
}
Resultimpl. Java
Public ClassResultimplImplementsResult {
PrivateString name;
PublicResultimpl (string name ){
This. Name = Name;
}
Public VoidExecute (){
System.Out. Println ("result" +This. Name + "Execute! ");
}
}
The test class is as follows:
Public ClassTest {
/**
*@ ParamARGs
*@ ThrowsException
*/
Public Static VoidMain (string [] ARGs)ThrowsException {
//TodoAuto-generated method stub
Arraylist interceptors =NewArraylist ();
For(IntI = 0; I <5; I ++ ){
Interceptor inter =NewInterceptorimpl ("Interceptor _" + I );
Interceptors. Add (inter );
}
Result result =Null;
Map resultmap =NewHashmap ();
Result =NewResultimpl ("success ");
Resultmap. Put ("success", result );
Result =NewResultimpl ("error ");
Resultmap. Put ("error", result );
Action action =NewActionimpl ();
Invocation invocation =NewInvocationimpl (interceptors, action, resultmap );
Actionproxy =NewActionproxyimpl (Invocation );
Actionproxy.exe cute ();
}
}
Key points:
1. comman Mode
2. IOC Injection
3. recursive call (using recursive call to implement method interception is a novel method)