ActionServlet in-depth discussion - 2

xiaoxiao2021-03-06  110

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).

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

    New Post(0)