Struts study notes 2 -ActionServlet in-depth discussion (reference)

xiaoxiao2021-03-06  69

Java.lang.object

|

- javax.servlet.GenericServlet

|

- javax.servlet.http.httpservlet

|

- Org.apache.struts.Action.ActionServlet

Struts provides a default version of the ActionServlet class, you can inherit this class, overwriting some of these methods to achieve your special processing needs. ActionServlet inherits with javax.servlet.http.httpservlet, so it doesn't matter in nature it and an ordinary servlet, you can look at it as a servlet, just in which the function is complete. ActionServlet mainly completes the following features:

Map an URI from the client to a corresponding Action class

If this Action class is called for the first time, then instantiate a cache if the corresponding ACTIONFORM is specified in the configuration file (Struts-config.xml), then capture the data fill the formBean call this action from the request of the data from Request The class's Perform () method, a reference to ActionMApping, corresponding ActionForm, and HTTPSERVLETREQUEST, HTTPSERVLETRESPONSE objects transmitted by the container to the ActionServlet.

Easy version of the ActionServlet reads the following initialization parameters from the configuration file Web.xml:

Application Application Use of Resources Bundle's base class Factory for creating the name of the MessageResourcesFactory of the application's MessageResources object. It is indeed Org.Pache.Struts.UTil.PropertyMessageResourcesFactory. Configstruts configuration files, which are omitted is /Web-inf/Struts-Config.xml. Note that this is a relative path associated with applying context. Content defines the provisional content type and encoding format, which will be automatically set to each Response if there is no clear setting in the JSP / Servlet. It is the provisions of TEXT / HTML. Debug debugging level. The default is 0, which is logged to the log file than the current level of debugging information. Detail is similar to Debug, but this detail is INitmapping () is dedicated. The debug information will be printed to System.out instead of a log file. FORMBEANACTIONFORMBEAN's implementation class, which is used as an ActionForward class used in org.apache.struts.Action.ActionFormBean Forward Applications, which is the Org.Apache.Struts.Action.ActionForward. Locale specifies the Locale object that is used to use. Set to True, when a session is obtained, a Locale object indicated by an action.locale_key is automatically stored in the session, and if the session has not been binded to an action.locale_Key. Locale object. The ActionMapping class used in mapping applications is ORG.Apache.Struts.Action.ActionMapping. MULTIPARTCLASS file Upload Used MutipartRequestHandler's implementation class. It is assumed to provoke org.apache.struts.upload.diskmultipartRutsthandler Nocache If set to true, the ActionServlet will automatically add Nocache's HTML header to the client's response so that the client will not cache the pages in the app. It is assumed that False Null is set to TRUE, then the application will return null when obtaining an undefined Message resource, rather than returning an error message. It is true that TRUE is safe. The size upper limit uploaded by the maxfilesize file is determined as the size of the buffer uploaded at the 250M BUFFERSIZE file, and it is determined to set the temporary directory for uploading the upload. The working directory will be provided as an attribute of a servlet environment. Validateare We use The New Configuration File Format to save TRUE. Validating is validated if the parsing configuration XML file is validation. Indeed for TRUE

The command design mode is applied in the ActionServlet.

When a servlet is generated by the container, first call the init () method to initialize, when connected to an HTTP request, call the corresponding method to process; such as a GET request calling the doget () method, the POST request calls the dopost () method. So first look at the init () method of the ActionServlet, you will be clear why ActionServlet can complete these features.

init ()

In its init () method, the ActionServlet calls the following protected method to complete the initialization:

INITACTIONS () - You may have this question: why can Struts find an Action class requesting the URI? The answer is here, the ActionServlet has an ActionS property that is org.apache.struts.util.fasthashmap, which is used to store the instantiated Action class that is fully named Key. When init () is called the initActions () method, in this method, it is just a simple clearance of all the names in Map, Synchronized (actions); Actions.clear (); Actions .Setfast (TRUE);} First set the Actions to a SLOW mode, then access to FastHashMap is the thread synchronization, then clear all existing name / value pairs in the Action, and finalize the Actions mode to FAST. . Since FasthashMap is an extension class based on Java.util.hashmap, it is necessary to adapt to multi-threads and most of the access to HashMap is the needs of read-only special environments. Everyone knows java.util.hashmap is non-thread safe, so HashMap is generally suitable for single-threaded environments. Org.apache.struts.fasthashmap is inherited in java.util.hashmap, which adds multi-threaded support. Working in FAST mode is this: reading is a non-thread synchronization; first clone the current map, then do writing exercises on this clone, replacing the original MAP after completion . So when will I add the Actions class to this map? We have mentioned that Struts is a dynamically generated instance of the Action class. When each ActionServlet receives a GET or POST HTTP request, the instance of the corresponding Action class will be found in this map. If there is no exist, then instance One, and put it in MAP. It can be seen that this Actions property has played a role of the cache for the Action class instance. InIntinternal () - Initializing the resource package for the ActionServlet, use MessageResources.getMessageResources (INTERNALNAME) to get the actionResources.Properties file corresponding to "org.apache.struts.Action.ActionResources". This resource package is mainly used for the prompt information during the ActionServlet process, which does not discuss it. INitDebug () - Read the debug level parameter getServletconfig (). GetInitParameter ("Debug"). INitApplication () - Initialize the application resource package and placed into the servletContext.

String factory = getServletConfig () getInitParameter ( "factory");. String oldFacory = MessageResourcesFactory.getFactoryClass (); if (! Factory = null) MessageResourcesFactory.setFactoryClass (factory);. String value = getServletConfig () getInitParameter ( "application") ; MessageResourcesFactory factoryObject = MessageResourcesFactory.createFactory (); application = factoryObject.createResources (value); MessageResourcesFactory.setFactory (oldFactory); getServletContext () setAttribute (Action.MESSAGES_KEY, application); Description: code fragment referenced herein may be omitted Some exception checks such as the content of the non-main line, please pay attention. First read the Factory parameter from the configuration file, if this parameter is not empty, then this specified Factory class is used in MessageResourcesFactory; otherwise, using the default factory class org.apche.struts.util.propertyMessageResourceFactory. Then call the static createFactory () method of MessageResourcesFactory to generate a specific MessageResourceFactory object (note: MessageResourcesFactory is an abstract class). This will call this specific MessageResource () method to get the resource file defined in the configuration file (Web.xml). The above Application object type is MessageResources. You can specify a specific factory class when configuring an ActionServlet in Web.xml. You cannot directly MessageResources () method because this method is Abstract. Create factoryObject process is as follows: MessageResourceFactory factoryObject = MessageResourcesFactory.createFactory (); application = factoryObject.createResources (value);

  • initMapping () - has a protected property mapping information for the application initialization ActionServlet: mapping, a package of ActionMapping Object Collection Force to manage, find ActionMapping. Mappings is an instance of org.apache.struts.Action.ActivityMappings class. There are two main methods: addmapping (actionmapping mapping) and FindMApping (String Path).

    ActionMapping is also the ORG.APache.Struts.util.fasthashmap class mentioned above to store all ActionMApping objects. mappings.setServlet (this); ...... // Initialize the name of our ActionFormBean implementation class value = getServletConfig () getInitParameter ( "formBean");. if (! value = null) formBeanClass = value; // Initialize the name of our ActionForward implementation class value = getServletConfig () getInitParameter ( "forward");. if (! value = null) forwardClass = value; // Initialize the name of our ActionMapping implementation class value = getServletConfig () getInitParameter ( "mapping").; IF (value! = null) mappingclass = value; in initmapping (), first link the mappings object to this servlet instance. In fact, this sentence is very simple, there will be an ActionServlet type attribute in ActionMappings, which defines the ActionServlet to which this ActionMappings object belongs. Struts is more flexible, where ActionFormBean, ActionForward, ActionMapping class You can customize Struts's working methods using your own subclasses. The above code reads the FORMBEAN, Forward, Mapping parameters from the configuration file (web.xml), which is the ActionFormBean, ActionForward, ActionMApping class name you customized.

    // Initialize the context-relative path to our configuration resources value = getServletConfig () getInitParameter ( "config");. If (! Value = null) config = value; // Acquire an input stream to our configuration resource InputStream input = getServletContext () .getResourceAsStream (config); Digester digester = null; digester = initDigester (detail); try {formBeans.setFast (false); forwards.setFast (false); mappings.setFast (false); digester.parse (input); mappings.setFast (true); forwards.setFast (true); formBeans.setFast (true);} catch (SAXException e) {throw new ServletException (internal.getMessage ( "configParse", config), e);} finally {input .close ();} Read the position of the Struts configuration file from Web.xml. Use org.apache.struts.digester.digester to parse the configuration file indicated by the config parameter, usually "/Web-inf/Struts-config.xml", resolve all Data-Source, Form-bean, action-maping, forward .

    As seen from the previous program, Digester only calls a parse () method, how does Digester use the Struts-Config.xml file and store the resolved result for FORM-BEAN to the attribute variable FORMBEANS, etc. ? You can notice the initdigester () method before calling Digester.Parse (InputStream): Digester Digester = New Digester (); Digester.push (this); Digester.addObjectCreate ("struts-config / action-mappings / action ", mappingClass," className "); digester.addSetProperties (" struts-config / action-mappings / action "); digester.addSetNext (" struts-config / action-mappings / action "," addMapping "," org. Apache.struts.Action.Actionmapping "); Digester.addSetProperty (" struts-config / action-mapings / action / set-property "," proty "," value "); in this method, first become a Digester object, then Set the rules and callbacks of the resolution, if you are not very familiar with XML, SAX is not very familiar, here you don't have to entangle too deep. It should be noted that the addsetNext () method sets each of the SET NEXT callback methods to parse elements, and this method is provided by the father of the Digester parser. "AddmApping" in the top of the above is a method defined by the ActionServlet itself will be called by Digester. Digester is to store each FormBean, ActionForward, ActionMapping, etc. of the parsed, etc., forwards, mappings, and the like. InIntupload () - Initialize some parameters for UPLOAD, such as buffersize, tempdir. InitDataSource () - Removes every DataSource read from the configuration file in initmapping (), sets the logwriter, if you are genericDataSource instance, turn on the data source. Then, put each DataSource into the context.

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

    New Post(0)