In-depth introduction to the Struts Framework (8): Analysis of Struts framework Example 3

Source: Internet
Author: User

The previous blog introduced three methods completed during the init method instantiation of actionservlet, including the initinternal () method, initother () method, and initservlet () method () methods.


Through the introduction of the previous blog, I learned that the initinternal () method mainly creates a resource class messageresources, such as international resources. For details, refer to Org. apache. struts. actionresources. properties file; initother () method to initialize other configuration, get the path of our own Struts-config configuration file, and its default path is web-INF/struts-config.xml, in addition, this method registers some conversion classes. The initservlet () method mainly uses digester to parse the web. configuration content in XML.


Today, we will continue to explain other things in the init method. First, let's talk about the specific implementation code of the init method so that you can easily read and understand it.


Init source code:

Public void Init () throws servletexception {</P> <p> try {<br/> // initialize the resource class <br/> initinternal (); <br/> // register the conversion class <br/> initother (); <br/> // use digester to read the web. XML file and put it in servletcontext <br/> initservlet (); <br/> getservletcontext (). setattribute (globals. action_servlet_key, this); </P> <p> initmoduleconfigfactory (); <br/> moduleconfig = initmoduleconfig ("", config); <br/> initmodulemessageresour CES (moduleconfig); <br/> initmoduledatasources (moduleconfig); <br/> initmoduleplugins (moduleconfig); <br/> moduleconfig. freeze (); </P> <p> enumeration names = getservletconfig (). getinitparameternames (); <br/> while (names. hasmoreelements () {<br/> string name = (string) names. nextelement (); <br/> If (! Name. startswith ("config/") {<br/> continue; <br/>}< br/> string prefix = Name. substring (6); <br/> moduleconfig = initmoduleconfig <br/> (prefix, getservletconfig (). getinitparameter (name); <br/> initmodulemessageresources (moduleconfig); <br/> initmoduledatasources (moduleconfig); <br/> initmoduleplugins (moduleconfig); <br/> moduleconfig. freeze (); <br/>}</P> <p> This. initmoduleprefixes (this. getservletcontext (); </P> <p> This. destroyconfigdigester (); <br/>} catch (unavailableexception ex) {<br/> throw ex; <br/>} catch (throwable t) {<br/> log. error ("unable to initialize struts actionservlet due to an" <br/> + "unexpected exception or error thrown, so marking the" <br/> + "Servlet as unavailable. most likely, this is due to an "<br/> +" incorrect or missing library dependency. ", T); <br/> throw new unavailableexception (T. getmessage (); <br/>}< br/>}

Getservletcontext (). setattribute (globals. action_servlet_key, this); this statement stores the actionservlet instance with globals. action_servlet_key as the key in servletcontext.


Here, globals. action_servlet_key has been declared in actionservlet:

Public static final string action_servlet_key = <br/> "org. Apache. Struts. Action. action_servlet ";

Next, the initmoduleconfigfactory () method is used to parse the configfactory text value in Web. xml. If configfactory is configured, The factoryclass value in moduleconfigfactory is set; otherwise, defamoduleconfigfactory is used by default. In fact, the purpose of this method is to allow developers to develop moduleconfigfactory themselves to obtain the moduleconfig class they need. Because this parameter is not configured in our instance, the instance here is about defalutmodelconfigfactory.


Code snippet 1:

Protected voidinitmoduleconfigfactory () {<br/> string configfactory = getservletconfig (). getinitparameter ("configfactory"); <br/> If (configfactory! = NULL) {<br/> moduleconfigfactory. setfactoryclass (configfactory); <br/>}< br/>}

Code Segment 2:

Public static void setfactoryclass (string factoryclass) {<br/> moduleconfigfactory. factoryclass = factoryclass; <br/> moduleconfigfactory. clazz = NULL; <br/>}

Code snippet 3:

Protected static string factoryclass = <br/> "org. Apache. Struts. config. impl. defaultmoduleconfigfactory"; <br/>}

The moduleconfig = initmoduleconfig ("", config) method is very important. The initmoduleconfig method initializes the attributes in strits-config and puts them in the moduleconfig object, put it in the moduleconfig object to facilitate later operations, because it is a file stream.


Specific implementation code:

Protected moduleconfig initmoduleconfig (stringprefix, string paths) <br/> throws servletexception {</P> <p> //: fixme: Document unavailableexception? (Doesn't actually throw anything) </P> <p> If (log. isdebugenabled () {<br/> log. debug (<br/> "Initializing module path'" <br/> + prefix <br/> + "'configuration from'" <br/> + paths <br/> + "'"); <br/>}</P> <p> // parse the configuration for this module <br/> moduleconfigfactory factoryobject = moduleconfigfactory. createfactory (); <br/> moduleconfig Config = factoryobject. createmoduleconfig (prefix); </P> <p> // configure the digester instance we will use <br/> digester = initconfigdigester (); </P> <p> // process each specified resource path <br/> while (paths. length ()> 0) {<br/> digester. push (config); <br/> string Path = NULL; <br/> int comma = paths. indexof (','); <br/> If (comma> = 0) {<br/> Path = paths. substring (0, comma ). trim (); <br/> paths = paths. substring (comma + 1); <br/>} else {<br/> Path = paths. trim (); <br/> paths = ""; <br/>}</P> <p> If (path. length () <1) {<br/> break; <br/>}</P> <p> This. parsemoduleconfigfile (digester, PATH); <br/>}</P> <p> getservletcontext (). setattribute (<br/> globals. module_key + config. getprefix (), <br/> config ); </P> <p> // force creation and registration of dynaactionformclass instances <br/> // For all dynamic form beans we wil be using <br/> formbeanconfig FBS [] = config. findformbeanconfigs (); <br/> for (INT I = 0; I <FBS. length; I ++) {<br/> If (FBS [I]. getdynamic () {<br/> FBS [I]. getdynaactionformclass (); <br/>}</P> <p> return config; <br/>}

It is necessary to parse this code. First, obtain the implementation class that inherits moduleconfigfactory. If factoryclass attribute can be set in initmoduleconfigfactory (), the custom factory can be generated. Otherwise, the default defaultleconfigfactory class is obtained, and the moduleconfigimpl class is obtained for the factory. Then call initconfigdigester () to prepare for parsing the configuration file and initialize the digest class (the initialization implementation of the specific Digest will not be explained ). Finally, moduleconfig is returned, and moduleconfig encapsulates information in all the struts-config.xml.


The last few methods are just a few simple words, which is not very difficult to understand:

The initmodulemessageresources (moduleconfig) method creates a messageresource object through the configuration file information in moduleconfig.

The initmoduledatasources (moduleconfig) method uses the configuration file information in moduleconfig to create the datasource object. initmoduleplugins (moduleconfig) to load and initialize all the ins of the default application module.

Moduleconfig. Freeze () is to set each object in the configuration file to the CONFIGURED state.


Finally, we can see that there is still a circular code similar to the above. This Code mainly means that after the default sub-application module is successfully initialized, if the application includes other sub-application modules, repeat the process to initialize other sub-application modules. This is also very understandable.


So far, actionservlet completes init.

 

After analyzing the source code of these two blogs, we can clearly understand why the configuration information is like that in the web. xml file, and how the program initializes actionservlet. The next blog explains how actionservlet intercepts strings. Stay tuned

 

 

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.