In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "WebWork framework initialization process and user request processing process", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "WebWork framework initialization process and user request processing process" it!
I. the framework initialization process of WebWork
The project done by WebWork completes the framework initialization of WebWork when the server starts. This is done through the init (ServletConfig servletConfig) method in the com.opensymphony.xwork.dispatcher.ServletDispatcher (FilterDispatcher) filter configured in Web.xml.
And the mapping of ServletDispatcher is configured in web.xml. When the user requests the browser with the mapped ending resource, ServletDispatcher will process the request (ServletDispatcher is a HttpServlet).
The concrete implementation is through the following steps:
1. Initialize the framework through the init method in ServletDispatcher:
Public void init (ServletConfig servletConfig) throws ServletException {super.init (servletConfig); DispatcherUtils.initialize (getServletContext ();}
2. The init method also calls the initialize method of the DispatcherUtils class to create the DispatcherUtils instance, and indirectly calls the init method of the DispatcherUtils class to initialize the Configuration configuration and create the factory ObjectFactory and ObjectTypeDeterminer created by the object.
This completes the initialization of the WebWork framework.
II. User request processing process of WebWork
All service requests ending with the mapping ServletDispatcher in web.xml will be processed by ServletDispatcher.
1. Resolve the name of the corresponding Action from the service name requested by the user.
Public void service (HttpServletRequest request, HttpServletResponse response) throws ServletException {/ /.... Try {request = du.wrapRequest (request, getServletContext ());} catch (IOException e) {String message = "Could not wrap servlet request with MultipartRequestWrapper!"; LOG.error (message, e); throw new ServletException (message, e);} du.serviceAction (request, response, getServletContext (), mapping);}
2. Traverse the data in HttpServletRequest, HttpSession and ServletContext, and copy it to the Map of Webwork, laying the foundation for the next step of creating Action instance.
Implementation: complete the encapsulation of the above information by calling Map extraContext = createContextMap (request, response, mapping, context) in the serviceAction method of DispatcherUtils.
3. Call ActionProxyFactory to create the corresponding ActionProxy instance with the encapsulated information in the previous step as parameters. ActionProxyFactory will create an instance of ActionProxy according to the settings in the Xwork configuration file (xwork.xml). The ActionProxy contains the configuration information of Action (including Action name, corresponding implementation class, etc.).
Implementation: through ActionProxy proxy = ActionProxyFactory.getFactory (). CreateActionProxy (namespace, name, extraContext, true, false); / / create a dynamic proxy DefaultActionProxyFactory to implement the createActionProxy method of ActionProxyFactory and return new DefaultActionProxy (namespace, actionName, extraContext, true, true); DefaultActionProxy is the default implementation of ActionProxy, which instantiates DefaultActionProxy through the DefaultActionProxy (namespace, actionName, extraContext, true, true) constructor of class DefaultActionProxy, and gets the actionName and namespace requested by the user, and through config = ConfigurationManager.getConfiguration (). GetRuntimeConfiguration (). GetActionConfig (namespace, actionName)
ConfigurationManager's
Public static synchronized Configuration getConfiguration () {if (configurationInstance = = null) {configurationInstance = new DefaultConfiguration (); try {configurationInstance.reload ();} catch (ConfigurationException e) {configurationInstance = null; throw e;}} else {conditionalReload () } return configurationInstance;}
Finish reading the configuration information of xwork.xml (the specific operation class is XmlConfigurationProvider). Get the ActionConfig associated with this request.
4. ActionProxy creates the corresponding Action instance and carries out a series of processing programs according to the configuration.
Through the invocation = ActionProxyFactory.getFactory () .createActionInvocation (this, extraContext) of the DefaultActionProxy class
/ / create an action call class ActionInvocation through the createActionInvocation method to handle the methods called by Action
Privatevoid resolveMethod () {/ / if the method is set to null, use the one from the configuration / / if the one from the configuration is also null, use "execute" if (! TextUtils.stringSet (this.method)) {this.method = config.getMethodName (); if (! TextUtils.stringSet (this.method)) {this.method = "execute" }}}
Then call the serviceAction method of DispatcherUtils
If (mapping.getResult ()! = null) {Result result = mapping.getResult (); result.execute (proxy.getInvocation ());} else {proxy.execute ();}
Completes the user's final action method to execute.
Public String execute () throws Exception {ActionContext nestedContext = ActionContext.getContext (); ActionContext.setContext (invocation.getInvocationContext ()); String retCode = null; try {retCode = invocation.invoke ();} finally {if (cleanupContext) {ActionContext.setContext (nestedContext);}} return retCode }
The ActionContext object is finally processed, and the Action call is submitted to ActionInvocation for processing.
5. Once the Action method returns, ActionInvocation will find the result corresponding to the result code (Action Result Code) of the Action (a String such as success, input) in the xwork.xml file, and then execute the result. Typically, result calls the JSP or FreeMarker template to render the page. When rendering the page, the template can use some of the tags provided by WebWork, some of which components can work with ActionMapper to render the appropriate URL for subsequent requests.
Let's look at the definition of the action section:
/ common/loginedHomeAction!init.action
The result node here has a type attribute, which indicates how the result of this action should be handled.
Let's take a look at how result of type dispatcher is defined:
At this point, you can see that the processing is left to the ServletDispatcherResult class.
The ServletDispatcherResult class inherits the WebWorkResultSupport class, while WebWorkResultSupport implements the com.opensymphony.xwork.Result interface, which is used to handle the results of action. The WebWorkResultSupport class defines an abstract method, doExecute, which is used to implement the processing of Result.
Let's take a look at what ServletDispatcherResult does:
Public void doExecute (String finalLocation, ActionInvocation invocation) throws Exception {PageContext pageContext = ServletActionContext.getPageContext (); if (pageContext! = null) {pageContext.include (finalLocation);} else {HttpServletRequest request = ServletActionContext.getRequest (); HttpServletResponse response = ServletActionContext.getResponse (); RequestDispatcher dispatcher = request.getRequestDispatcher (finalLocation) / / if the view doesn't exist, let's do a 404 if (dispatcher = = null) {response.sendError (404, "result'" + finalLocation + "'not found"); return } / / If we're included, then include the view / / Otherwise do forward / / This allow the page to, for example, set content type if (! response.isCommitted () & & (request.getAttribute ("javax.servlet.include.servlet_path") = = null) {request.setAttribute ("webwork.view_uri", finalLocation) Request.setAttribute ("webwork.request_uri", request.getRequestURI ()); dispatcher.forward (request, response);} else {dispatcher.include (request, response);}
We see that the final call is dispatcher.forward (request, response); so we can successfully go to our target page.
The following code is for the serviceAction method in DispatcherUtils:
Public void serviceAction (HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {Map extraContext = createContextMap (request, response, mapping, context); OgnlValueStack stack = (OgnlValueStack) request.getAttribute ("webwork.valueStack"); if (stack! = null) extraContext.put ("com.opensymphony.xwork.util.OgnlValueStack.ValueStack", new OgnlValueStack (stack)); try {String namespace = mapping.getNamespace () String name = mapping.getName (); String method = mapping.getMethod (); String id = request.getParameter ("_ continue"); if (id! = null) {Map params = (Map) extraContext.get ("com.opensymphony.xwork.ActionContext.parameters"); params.remove ("_ continue") ExtraContext.put ("_ continue", id);} ActionProxy proxy = ActionProxyFactory.getFactory (). CreateActionProxy (namespace, name, extraContext, true, false); proxy.setMethod (method); request.setAttribute ("webwork.valueStack", proxy.getInvocation (). GetStack ()); if (mapping.getResult ()! = null) {Result result = mapping.getResult () Result.execute (proxy.getInvocation ());} else {proxy.execute ();} if (stack! = null) request.setAttribute ("webwork.valueStack", stack);} catch (ConfigurationException e) {LOG.error ("Could not find action", e) SendError (request, response, 404, e);} catch (Exception e) {String msg = "Could not execute action"; LOG.error (msg, e); throw new ServletException (msg, e);}}
Third, the execution flow chart of WebWork
At this point, I believe that everyone on the "WebWork framework initialization process and user request processing process" have a deeper understanding, might as well to actual operation it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.