WebWork2 source code analysis

xiaoxiao2021-03-06  18

Author: zhuam

Last night, I read the << Webwork2_Guide >>, whose documentation was very simple, but still lost as a good Webwork2 book, and the experience and ability of the players were very old, here to the author Source spirit, tribute, and hereby quoting the words of Xia Zhen So Many Open Source Projects, Why NOT OPEN YOUR Documents?

Today, I downloaded the latest webwork2 version. I started the source code. This document can only be considered my personal notes, and there is no time to make a detailed school. And the personal ability is limited, there is a lot of analytical errors or misunderstandings. .

The web1.x version is a tightening architecture, similar to Struts, and the webwork2.x version uses a new architecture webwork2.x xwork1.x because he uses such an architecture, so attracting me to see his source code. The new architecture has separated the tight coupling with the Servlet API, so our system structure will be more clear, and the system testing and system transplant will be more convenient. In fact, I have never seen the WebWork1.x version, the so-called fastead is also Seeing this online, there is no say, huh, huh!

The WebWork2.x's role in the new architecture is responsible for separating the user's HTTP request, making the request completely from the servlet API, then incoming these requests to the MAP to incorporate xwork1.x, and xwork1.x will be through Interceptor The data in the map is transmitted to our VO and then calls the corresponding custom Action.

Let's first look at the web2.x how to separate the HTTP request, in fact, he is achieved by servletdispatcher. The code is as follows:

Public Static HashMap CreateContextMap (Map Requestmap, Map ParameterMap, Map SessionMap, Map ApplicationMap, HttpservletRequest Request, HttpservletResponse Response, ServletConfig Servletconfig) {

Hashmap extracontext = new hashmap ();

/ / Store the Request session request for uploading files in HTTP

EXTRACONTEXT.PUT (ActionContext.Parameters, ParameterMap);

EXTRACONTEXT.PUT (ActionContext.Session; sessionMap);

EXTRACONTEXT.PUT (ActionContext.Application, ApplicationMap);

EXTRACONTEXT.PUT (ActionContext.locale, (locale == null) Request.getlocale (): locale);

EXTRACONTEXT.PUT (http_request, request);

EXTRACONTEXT.PUT (http_response, response);

EXTRACONTEXT.PUT (servlet_config, servletconfig);

EXTRACONTEXT.PUT (ComponentInterceptor.component_manager, request.getattribute (ComponentManager.comPonent_manager_key));

// helpers to get access to request / session / application scope

/ / Here is used to store Request session requests in HTTP

EXTRACONTEXT.PUT ("Request", Requestmap;

EXTRACONTEXT.PUT ("session", sessionmap;

EXTRACONTEXT.PUT ("Application", ApplicationMap; EXTRACONTEXT.PUT ("Parameters", ParameterMap;

AttributeMap attrmap = new attributemap (extracontext);

EXTRACONTEXT.PUT ("Attr", AttrMap;

Return ExtraconText;

}

The above is the method CreateContextMap in servletdispatcher is used to encapsulate the HTTP request to the map, and servletdispatcher is also the controller of the webwork2.x, responsible for separating the action, he is calling the serviceAction method through the service method, then completed by serviceAction The operation, the code is as follows:

Public Void ServiceAction (HTTPSERVLETREQUEST, HTTPSERVLETRESPONSE, STRING NAMESPACE, STRING ActionName, Map Requestmap, Map Prametermap, Map Sessionmap, Map ApplicationMap) {

Hashmap extracontext = createContextMap (Requestmap, ParameterMap, sessionmap, applicationmap, request, response, getservletconfig ()); // Instant MAP request

EXTRACONTEXT.PUT (servlet_dispatcher, this);

// if there is a previous value stack, the crete a new copy and pass it in to be buy by the new action

OgnvaluestAck Stack = (OGNLVALUESTACK) Request.getAttribute (servletActionContext.WebWork_ValuestAck_Key);

IF (stack! = null) {

EXTRACONTEXT.PUT (ActionContext.Value_stack, New OgnvalUStack);

}

Try {

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

/ / Here, ActionName is parsed by two getActionName. The following is the Todo: Questions reproduced to let everyone look at it.

* 1) What unit is MaxSize of attachments in? (Assuming Bytes for now)

* 2) Isn't Error Message WRONG IN CATCH OF TRY / CATCH IN Service () METHOD?

* 3) why is getActionname (string) Not declared public? (The Fix Would Not Be An Api Addition SO this Could Be (Done for pre 2.1)

* 4) why does createcontextMap (...) RETURN A HashMap and not a map? (2.1 API Change)

* 5) why doesn't getnamespace (request) Get the servlet path in The Same Way That GetActionName (Request) Does? * 6) Why does getParameterMap throw an iexception? Can't See a reason for That. (2.1 API Change)

* /

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

Proxy.execute ();

// if there is a previous value stack the set it back ONTO THE Request

IF (stack! = null) {

Request.setttribute (servletActionContext.WebWork_ValuestAck_Key, Stack);

}

} catch (configurationException e) {

Log.Error ("Could Not Find Action", E);

Senderror (Request, Response, httpservletResponse.sc_not_found, e);

} catch (exception e) {

Log.Error ("Could Not Execute Action", E);

Senderror (Request, Response, httpservletResponse.sc_internal_server_error, e);

}

}

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

New Post(0)