WEBWORK2 Source Analysis

Source: Internet
Author: User
Web
Author:zhuam







Last night, I read the Xia Xin written <<webwork2_guide>&gt, although the document is concise, but still a good WEBWORK2 books, see the author's experience and ability are very sophisticated, in this to the author's open source Spirit salute, And in this quote Xia Xin that sentence so many open source projects, Why not open your Documents?







Today, download the latest version of the WEBWORK2, began the source analysis, this document can only be my personal notes, there is no time to carefully proofread, and limited personal ability, it is inevitable that there are a lot of analysis errors or clerical mistakes, and I hope you correct me.







webwork1.x version is a tightly coupled architecture, similar to struts, and webwork2.x version after the adoption of a new architecture webwork2.x+xwork1.x is because he adopted such a framework so that I go to see his source! The new architecture separated the Servlet API with the tight coupling, so that our system structure will be more clear, and system testing and system transplant in the future more convenient, in fact, webwork1.x version I have not seen, the so-called tight coupling is to see the internet is so said, no say ah, hehe!



The role of webwork2.x in the new architecture is to detach the user's HTTP request, make the request completely detached from the servlet API, and then pass the request in map to the xwork1.x, and xwork1.x the map through interceptor The data in is passed to our VO and then called by the corresponding custom action.







The next step is to see how webwork2.x separates HTTP requests, but in fact he achieves them through servletdispatcher. The code looks like this:



public static HashMap Createcontextmap (map Requestmap, map Parametermap, map Sessionmap, map Applicationmap, HTTPSERVLETR Equest request, HttpServletResponse response, ServletConfig servletconfig) {



HashMap extracontext = new HashMap ();



Request session requests to store uploaded 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



This 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 in Servletdispatcher Createcontextmap is used to encapsulate HTTP requests to the map, and Servletdispatcher is also the webwork2.x controller, which is responsible for separating the action. He is using the service method to invoke the Serviceaction method, and then by Serviceaction to complete the operation, the code is as follows:







public void Serviceaction (HttpServletRequest request, httpservletresponse Response, String namespace, String ActionName , map Requestmap, map Parametermap, map Sessionmap, map Applicationmap) {



HashMap Extracontext = Createcontextmap (Requestmap, Parametermap, Sessionmap, Applicationmap, request, response,  Getservletconfig ()); Instantiating a map request



Extracontext.put (Servlet_dispatcher, this);



If There is a previous value stack, then create a new copy and pass it used by the new Action



Ognlvaluestack stack = (ognlvaluestack) request.getattribute (Servletactioncontext.webwork_valuestack_key);



if (stack!= null) {



Extracontext.put (actioncontext.value_stack,new ognlvaluestack (STACK));



}



try {



Actionproxy proxy = Actionproxyfactory.getfactory (). Createactionproxy (namespace, ActionName, Extracontext);



Here ActionName is through two getactionname parse out, the following is Servletdispatcher Todo:questions reproduced out to let everyone see



* 1) What is maxSize the attachments in? (assuming bytes for now)



* 2) ISN ' t error message wrong in catch the Try/catch in service () method?



* 3) Why is Getactionname (String) isn't declared public? (The fix would not is a API addition so could is (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)?



* 6) Why does Getparametermap throw an IOException? Can ' t a reason for that. (2.1 API Change)



*/



Request.setattribute (Servletactioncontext.webwork_valuestack_key, Proxy.getinvocation (). GetStack ());



Proxy.execute ();



If There is a previous value stack then set it back onto the request



if (stack!= null) {



Request.setattribute (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);



}



}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.