"Encapsulates an INPUT element in a Web page as a (request) data Object", an object that isThe actioninvocation type.
For Xwork, the webwork component of the front-end provides a data structure of a map type. The action is oriented to the data structure provided by the model object. When and where do you convert these two different data structures?
Write a helper class to do the work, and call him to complete the conversion work before each action call by the framework code.
Xwork This step through interceptor so that we can flexibly configure the required interceptor as needed. This provides an extensible preprocessing and post-processing process for the action.
actioninvocation is the core of action scheduling in Xworks. And the dispatch of Interceptor, is also the responsibility of actioninvocation .
The actioninvocation is an interface, and the defaultactioninvocation is webwork to actioninvocation The default implementation.
The Interceptor scheduling process is as follows:
1. when actioninvocation is initialized, all interceptor associated with the action are loaded according to the configuration.
See the actioninvocation. Init method for the relevant code: Java code:PrivatevoidInit ()throwsException ... {
......
List interceptorlist =New
ArrayList (Proxy.getconfig (). Getinterceptors ());
Interceptors2. Pass actioninvocation. when the Invoke method invokes the action implementation, the Interceptor is executed:
Here is theaction dispatch code in defaultactioninvocation :
Java code: PublicString <spanclass=undefined>invoke</span> ()throwsException ... {
if(executed)
ThrowNewIllegalStateException ("Action has already executed");
if(Interceptors.hasnext ()) ... {
Interceptor Interceptor = (Interceptor) interceptors.next ();
ResultCode = Interceptor.intercept ( This);
}Else
ResultCode = <spanclass=undefined>invoke</span>action (Getaction (), Proxy.getconfig ());
if(!executed) ... {
if(Preresultlisteners! =NULL) ... {
Iterator Iterator = Preresultlisteners.iterator ();
while(Iterator.hasnext ()) ... {
Preresultlistener Listener
= (Preresultlistener) iterator.next ();
Listener.beforeresult ( This, ResultCode);
}
}
if(Proxy.getexecuteresult ())
Executeresult ();
executed =true;
}
returnResultCode;
All interceptors must implement the Interceptor interface.
Public interface Interceptor {
void Destroy ();
void Init ();
String Intercept ( actioninvocation invocation) throws Exception;
}
In the interceptor implementation, the abstract implementation Aroundinterceptor is the most widely used (extended), which increases the definition of preprocessing (before) and post-processing (after) methods.
Aroundinterceptor.java:Java code: PublicAbstract classAroundinterceptorImplementsInterceptor
... {
protectedLog log = Logfactory.getlog ( This. GetClass ());
PublicvoidDestroy () ... {
}
PublicvoidInit () ... {
}
PublicString Intercept (<spanclass=hilite1>actioninvocation</span> invocation)throwsException ... {
String result =NULL;
Before (invocation);
result = Invocation.<spanclass=undefined>invoke</span> ();
After (invocation, result);
returnResult
}
protectedAbstract voidAfter
(<spanclass=hilite1>actioninvocation</span> <spanclass=hilite1>actioninvocation</span>, string string)throwsException;
protectedAbstractvoidBefore (<spanclass=hilite1>actioninvocation</span> <spanclass=hilite1>actioninvocation</span>)
throwsException;
}aroundinterceptor.in the Invoke method, the Invoke method of the parameter invocation is called .
Finally, combine the most commonly used parametersinterceptor to see how xwork through Interceptor, convert the WebWork incoming map type data structure to the Java model object required by the action.
Parametersinterceptor.java:
Java code: PublicclassParametersinterceptorextendsAroundinterceptor ... {
protectedvoidAfter (<spanclass=hilite1>actioninvocation</span> Dispatcher, String result)
throwsException ... {
}
protectedvoidBefore (<spanclass=hilite1>actioninvocation</span> invocation)throwsException
... {
if(! (Invocation.getaction ()instanceofnoparameters)) ... {
FinalMap parameters =
Actioncontext.getcontext (). GetParameters (); ⑴
if(log.isdebugenabled ()) ... {
Log.debug ("Setting params"+ parameters);
}
Actioncontext Invocationcontext =
Invocation.getinvocationcontext ();
Try... {
Invocationcontext.put (
Instantiatingnullhandler.create_null_objects,
Boolean.true);
Invocationcontext.put (
Xworkmethodaccessor.deny_method_execution,
Boolean.true);
Invocationcontext.put (
Xworkconverter.report_conversion_errors,
Boolean.true);
if(Parameters! =NULL) ... {
FinalOgnlvaluestack stack =
Actioncontext.getcontext (). Getvaluestack (); ⑵
for(Iterator Iterator =parameters.entryset (). Iterator ();
Iterator.hasnext ();
) ... {
Map.entry Entry = (map.entry) iterator.next ();
Stack.setvalue (⑷
Entry.getkey (). toString (),
Entry.getvalue ());
}
}
}finally... {
Invocationcontext.put (
Instantiatingnullhandler.create_null_objects,
Boolean.false);
Invocationcontext.put (
Xworkmethodaccessor.deny_method_execution,
Boolean.false);
Invocationcontext.put (
Xworkconverter.report_conversion_errors,
Boolean.false);
}
}
}
}
Parametersinterceptor extends the abstract class Aroundinterceptor. And the data conversion is realized in the Preprocessing method (before).
The process of data conversion is not complicated:
⑴ first obtains the map type parameter set parameters by the Actioncontext.
⑵ Gets the value stack (ognlvaluestack) by Actioncontext.
The ⑶ iterates through the data in the parameters.
⑷ populates the Model object with attribute data by Ognlvaluestack, based on the key value of the data.
Ognlvaluestack is a class library that Http://www.ognl.org4 provides a set of read-write Object properties
The above code does not find the part of the model object in the stack, because the actioninvocation in the initialization of the pre-completion of the stack work, such as the defaultactioninvocation the code in the. Init method shows:
private void Init () throws Exception {
Map Contextmap = Createcontextmap ();
CreateAction ();
if (pushaction) {
Stack.push (action); Press Stack
}
......
}
WebWork Interceptor Interceptor Actioninvocation