This article details how struts handle a request after initialization and return data. The core class here is RequestProcessor and Requestutils. The RequestProcessor class implements the jump of the page via the RequestDispatcher, and the requestProcessor is responsible for processing the request information from the request, stored in the FormBeanconfig, and processes the URL to be jumped. Struts After the initialization is completed, the Doget (...) or dopost (...) method is called according to the request, and the two methods directly call the Process (Request, Response) method. Process (...) method first determines which ModuleConfig current Request belongs and then generates the requestProcessor corresponding to this moduleconifg, and finally calls this requestProcessor's Process (...) method to perform Request's request. 1. RequestUtils.SelectModule (String Prefix, httpservletRequest, servletContext) method: This method, according to Prefix, select the corresponding moduleconfig from servletContext, then save this moduleconfig to the Request. The Key value corresponding to servletContext is globals.module_key prefix, saved to the key value used in Request to globals.Module_Key. If there is no such moduleconfig in servletContext, then call the request.removeattribute (Globals.Module_Key) method. Then look up in the same method, save the MessageResources object. When prefix is empty, the following method will be called to select ModuleConfig. Two, RequestUtils.selectModule (HttpServletRequest, ServletContext) sent us this first use getModuleName (HttpServletRequest, ServletContext) obtain the appropriate path, and then get the appropriate prefix by calling getModuleName (String matchPath, ServletContext). Select ModuleConfig in SelectModule (...) in this prefix. The process of getting the path is: First find the properties of the name INCLUDE_SERVLET_PATH (Javax.Servlet.InClude.Servlet_path) from the request_Path, if you empty, call the request.getServletPath () method to get servletpath. Get the process of Prefix is: it first calls getModulePreFixes (servletContext), gets all existing Module prefixes. Then, by analyzing the previously acquired path which module belongs to which Module belongs to the last "/" character in front of the PATH, then compares the prefixes [] obtained in the above method, if prefixes [ The existence of such a value in this, returns this intercepted string, otherwise continue to intercept the "/" front of the last "/" of the PATH, and then compare. Returns "" if you can't find it.
GetModulePrefixes (servletContext) is: first lookup if there is such a String Array if there is such a String Array if there is, if you exist, you have paid once, you will return this string array, otherwise Traversing all Attribute Name in ServletContext, find the Atribute Name starting with module_key (org.apache.struts.Action.module), then intercept the string behind Module_Key in this Atribute Name, as a prefix. Take this way All prefix is stored as an ArrayList in servletContext, the property Key value is prefixes_key (org.apache.struts.util.prefixes), so you can get it directly from this Attribute when you look up again. Third, getModuleConfig (httpservletRequest) This method is to get the moduleconfig obtained by the SELECTMODULE (...) method. If no such ModuleConfig, then put the ServletContext default ModuleConfig return (call getServletContext (). GetAttribute (Globals.MODULE_KEY)) four, getRequestProcessor (ModuleConfig config) The method according to ModuleConfig from the prefix as a key, from ServletContext Get the RequestProcessor in. This Key value is globals.request_processor_key config.getprefix (). If there is no such requestProcessor in ServletContext, then generate a new RequestProcessor instance, complete the initialization (saving the ActionServlet, and ModuleConfig to this new instance), saving it to servletContext. 5. RequestProcessor.Process (httpservletRequest, httpservletResponse) This is a method that is really executing HTTPSERVLETREQUST request. This method first judges whether the HTTPSERVLETREQUEST is a multipart type Request, that is, whether the current Request is field is the "file" type. The reason for this is that the similar approaches in this type of Requester () are unable to do, so they are distinguished. If it is a multipart type, then this Request package is loaded into the MultipArtRequestWrapper class. MultipartRequestWrapper is different from httpservletRequest to re-implement setParameter (String name, string value), getParameter (), getParameterNames (), and getParameterVale (String Name) method.
The idea of these methods is all variable names, and the value is saved to a HashMap: Key is the variable name, the value is a variable value, but it is not a string [] format. When there is a new value to join, add the value to the finals of the array through the setParameter (...) method. There is a relatively rare preservation value in SetParameter (...), recorded as follows: public void setParameter (String name, string value) {string [] mvalue = (String []) parameters.get (IF); if (mvalue == null) {mvalue = new string [0];} String [] newValue = new string [mvalue.length 1]; System.ArrayCopy (Mvalue, 0, NewValue, 0, Mvalue.Length); NewValue [ Mvalue.Length] = value; parameters.put (name, newvalue);} then calls ProcessPath (httpservletRequest, httpservletResponse) Get address, with this address as a key from ModuleConfig to get ActionMApping. This first queries if there is such a value in Request.GetaTribute (include_path_info), if you are empty, call the request.getPathInfo () method to get the PATH. If this is not obtained, get PATH from the request.gettribute (Include_Servlet_path) method, not finding the PATH obtained using Request.GetServletPath (). The analysis process is as follows: Then if this PATH is not a current moduleconfig, return NULL directly. Intercept the string behind Prefix, if this string ends in .xx, then intercept ".", The characters in front, such as http: // localhost: 8080 / servlet / where / go.do. Where is the name of Module (Prefix), then our Path value is / Go. Local then set a target for the current user by processLocale (HttpServletRequest, HttpServletResponse), it looks for the order is:. ModuleConfig.getControllerConfig () getLocale (), session.getAttribute (Globals.LOCALE_KEY), request.getLocale (). You can perform related definitions via
You can perform related definitions via
The ActionForm and FORMBEAN are initialized via ProcessPopulate (HTTPSERVLETRESPONSE, ACTIONFORM, ActionMApping) method, which first sets the ActionServlet belonging to this ActionForm and then initializes this ActionForm. Determine whether the current Reqest is multipart type, if you save the corresponding MultipartClass class to the Request, the Key value is globals.multipart_key. The repaStutils.Populate (...) is processed to the parameters in the request, and saved to the corresponding FORMBEAN. This method will be described in detail in the next article. Finally, based on the parameter of Request, whether the current request is removed, then saved the relevant information to Request: if ((Request.cancel_Property! = Null) || (Request.cancel_Property_x) ! = NULL)) {Request.SetaTRibute (Globals.cancel_Key, Boolean.true);} 6. The following explains how this request is truly processed. The following method is defined in RequestProcessor. Judging whether the parameter in the requestValidate (HTTPSERVLETRESPONSE, ACTIONFORM, ACTIONMApping) determines if the parameter in the Request is legal. If you return to TRUE if you are legal, the request is canceled and returned to the specified input page. It judges the legal process is: If the request is canceled, then directly return true; if there is no legality check for the parameter in the request, it also returns TRUE directly; finally by calling form.validate (mapping, request) Test, if the ActionerRors returned is NULL, then the representative returns TRUE by verifying, otherwise the Request is canceled. The canceled process is: If this request is a multipart type, then you want to roll back this MultipArtRequestHandler; then, get the current actionMApping INPUT value, that is, in the input ... /> defined in config XML, the INPUT property defined in Config XML. If ActionMApping does not have this input value, then call the response.senderror (...) method, then return false; save the verification error message to the request, KEY is Globals.Error_Key, jump to the address specified by INPUT .
processForward (HttpServletRequest, HttpServletResponse, ActionMapping) and processInclude (HttpServletRequest, HttpServletResponse, ActionMapping), respectively, by performing RequestDispatcher.forward (request, response) and RequestDispatcher.include (request, response) to achieve the jump to the page. But if there is any other operation to be executed if the current request is executed, the value of the include or Forward property in ActionMapping will be empty. At this time, you need to generate an Action class based on the configuration information of the Config XML file by calling the ProcessActionCreate (HTTPSERVLETREQUEST, the ActionMApping), and then calls the Execute (...) method in the generated action through ProcessctionPerform (...). Finally, the Jump is performed according to the ActionForWord class returned by the execute (...) method. There is a computational method of a URL in the entire process, which will be described in the next. 7. Description of the ActionMaping structure: A JavaBean saved by ActionConfig, which has the following properties: * Boolean configured is all over the object. If it has been completed, then in changing any properties will throw IllegalStateException ("Configuration IS Frozen") * ModuleConfig ModuleConfig This ActionConfig class is the moduleconfig. * String Attribute Request Range or Session Range Properties Name, we will get the corresponding Form Bean through this property name, note that this property is different from the name defined in the Form Bean. Note the following method: public string getttribute () {if (this.ttribute == null) {return (this.Name);} else {return (this.attribute);}} * string forward call requestdispatcher.forward () The context-related address required when the method is required. * String Include Call the RequestDispatcher.InClude () method you need for the context-related address. * The class of the String Type Action class, if the above two properties are not defined, it will be used to process the request. * String INPUT If the input data is not verified, it will be addressed to the corresponding page with this value. * String MultipartClass MultipartRequestHandler Implement the full name of the class * String Name and the name of the form bean related to this class. * String Parameter is used for Struts extension and stores other configuration information. Struts will not use this attribute.
* String Path Context related address, this address is the next address that will enter, this address must begin with "/". If extension mapping is used, this address will not have an extension. * String Roles One with "," separated role name. These characters will have permission to access this request. * String Scope defaults to session. * String Prefix, String Suffix suffix and prefix. * Boolean unknown flag The current ActionConfig class is prepared for unknown request path, and ActionMappings will return this ActionConfig based on this flag. * Boolean Validate does not call the validate () method for data checks. ActionMapping Inherits from ActionConfig, which adds methods from ModuleConfig to find an actionForWord. At the same time, it also provides a method from ModuleConfig to find ExceptionConfig. If you can't find it, you will continue to look for by the type.getsuperclass () method, according to the parent class name, until the end. 8. Explanation about request.getServletPath (): The return value of the request.getServletPath () is the value defined in the URL below, for example, if you are defined below