WebWork2 source code analysis

xiaoxiao2021-03-06  18

As for the creation of Action, it is done by ActionProxy to see a brief program call.

ActionProxy Proxy = ActionProxyFactory.getFactory (). CreateActionProxy (Namespace, ActionName, EXTRACONTEXT);

Request.setttribute (servletActionContext.webwork_valuestack_key, proxy.getinvocation (). getStack ()); // Call ActionInvocation

Proxy.execute ();

In fact ActionProxy is an interface, and ActionProxyFactory is an abstract class, they are by a DefaultActionProxy and DefaultActionProxyFactory to complete the operation, and will call ActionProxy ActionInvocation interface reads the configuration from the time DefaultActionInvocation initialized, and then by the Invoke () method Complete the action of Action and some operations of Interceptor before Action call. The following is the initialization and call code for DefaultActionInvocation.

Public class defaultActioninvocation imports actionInvocation {

Private vidinity () throws exception {

Map contextMap = createContextMap ();

CreateAction (); // Load Action

IF (push) {

STACK.PUSH (Action);

}

InvocationContext = New ActionContext (CONTEXTMAP);

InvocationContext.setName (Proxy.GetActionName ());

// Get a new list so we don't get problems with the iterator if someone changes the list

List interceptorlist = new arraylist (proxy.getconfig (). GetInterceptors ()); // Get configuration

Interceptors = interceptorlist.ITerator ();

}

Public string infoke () throws exception {

IF (executed) {

Throw New IllegalStateException ("Action Has Already Executed");

}

/ / Here is the operation of performing interceptors, Note: The interceptor itself is a special implementation of AOP, and Filter in servlet2.3 is a special case.

IF (interceptors.hasnext ()) {

Interceptor interceptor = (interceptor) interceptors.next ();

Resultcode = interceptor.intercept (this);

} else {

Resultcode = invokeaction (getAction (), proxy.getconfig ());

}

// this is neededed Because The Result Will Be Executed, THEN Control Will Return To The Interceptor, Which Will

// Return Above and Flow Through Again

IF (! executed) {

IF (presentlisteners! = null) {for (iTerator Iterator = preResultListeners.iterator ()

Iterator.hasnext ();) {

PrereSultListener Listener = (presentlistener) oreator.next ();

Listener.beforeResult (this, resultcode);

}

}

// now Execute the result, if we're supposed to

IF (proxy.getexecuteresult ()) {

ExecuteResult ();

}

Executed = true;

}

...

}

Let's talk about the implementation structure of Interceptor, just started to think that the interceptor in xwork1.x should be inherited from the Filter, and later I saw the source code. It turns out that my idea is wrong. I don't need it. I should not be from Filter inherits, because Filter is an API of servlet2.3, and the xwork1.x design is to be separated from the servlet API, and the interceptor is not unable, but we have Filter will come more convenient!

For all interceptors in WebWork2.x, they have a public interface interceptor, defined some basic operation methods of the interceptor, and then there is an AroundInterceptor abstraction class, which implements the interface. The role of AroundInterceptor is a combination. The interceptor call sequence, the code is as follows:

Public String Intercept (ActionInvocation Invocation) throws exception {

String result = null;

Before (Invocation); / / This is used for combined call sequence

Result = invocation.invoke ();

After (Invocation, Result);

Return Result;

}

As for transitioning data in Map to our VO, it is done through the ParametersInterceptor interceptor. This interceptor is a real implementation class, he inherits from the AroundInterceptor abstraction

Public class parametersInterceptor extendes aroundinterceptor {

// ~ Methods

Protected void after (ActionInvocation Dispatcher, String Result) throws Exception {

}

Protected void before (actioninvocation invocation) throws exception {

IF (! (invocation.getAction () instanceof noparameters)) {

Final Map parameters = actionContext.getContext (). getParameters ();

// Used to get Parameters in the MAP structure

IF (log.Indebugeload ()) {

Log.debug ("Setting Params" parameters;

}

ActionContext InvocationContext = Invocation.getInvocationContext ();

Try {

InvocationContext.Put (InstantiatingnullHandler.create_null_Objects, Boolean.true);

InvocationContext.put (xworkmethaccessor.de_method_execution, boolean.true); InvocationContext.put (xworkconverter.report_conversion_errors, boolean.true);

IF (parameters! = null) {

Final OgnvaluestAck Stack = actionContext.getContext (). getValUStack ();

// Used to get OgnwaStack operation, this package has never seen, the specific listening to the class library of readable object properties, features like Jakarta Commons Beanutils, and Spring Bean Wrapper

Iterator Iterator = parameters.Entryset (). Itrator ();

// Traverse the information in Parameters

Iterator.hasnext ();) {

Map.Entry entry = (map.entry) iterator.next ();

String name = entry.getKey (). TOSTRING ();

// Fill VO information

IF (AcceptableName (Name)) {

Object value = entry.getValue ();

Stack.setValue (Name, Value);

}

}

}

} finally {

InvocationContext.put (InstantiatingnullHandler.create_null_Objects, Boolean.false);

InvocationContext.put (xworkmethaccessor.de_method_execution, boolean.false);

InvocationContext.put (xworkconverter.report_conversion_errors, boolean.false);

}

}

}

Protected Boolean AcceptableName (String Name) {

IF (name.indexof ('=')! = -1 || Name.indexof (',')! = -1 || Name.indexof ('#')! = -1) {

Return False;

} else {

Return True;

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-41845.html

New Post(0)