In-depth introduction to the Struts Framework (7): Analysis of Struts framework Example 2

Source: Internet
Author: User

In the previous blog, I talked about how actionservlet is initialized and the specific role of Web. xml configuration information. Today we will continue to explain how actionservlet reads/WEB-INF/struts-config.xml information to memory during initialization and how to present the information read from the configuration file in the form of actionmapping in memory.

 

Because this part of content is cumbersome, I cannot analyze it in detail, but I will explain the specific implementation process clearly, if you are interested in the research, you can send me an email or Q me.

 

Next we will start today's blog, starting with the init method of the actionservlet source code. Because actionservlet is a servlet, it also has typical methods such as init, doget, and dopost. Since it is initialization, we need to look at the init method. The source code of the init method is as follows:

/** <Br/> * <p> initialize this servlet. most of the processing has been factored into <br/> * Support methods so that you can overrideparticle functionality at a <br/> * fairly granular level. </P> <br/> * @ exception servletexception if we cannotconfigure ourselves correctly <br/> */<br/> publicvoidinit () throwsservletexception {</P> <p> // wraps the entire initialization in a try/catch Better handle <br/> // unexpected exceptions and errors to provide better feedback <br/> // to the developer <br/> try {<br/> initinternal (); <br/> initother (); <br/> initservlet (); </P> <p> getservletcontext (). setattribute (globals. action_servlet_key, this); <br/> initmoduleconfigfactory (); <br/> // initialize modules as needed <br/> moduleconfig = initmoduleconfig ("", config ); <br/> in Itmodulemessageresources (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) {</P> <p> // The follow error message is not retrieved from internal message <br/> // resources as they may not have been able to have been <br/> // initialized <br/> log. error ("unable to initialize struts actionservlet due to an" <br/> + "unexpected exception or error thrown, so marking the" <br/> + "Servlet as unavailable. mostlikely, this is due to an "<br/> +" incorrect or missing library dependency. ", T); <br/> throw new unavailableexception (T. getmessage (); <br/>}< br/>}

 

Before explaining the process and meaning of this code, it is necessary to say that when we look at the code in eclipse, especially when we look at a piece of unfamiliar and long code, I hope to use the ctrl key frequently (not to be explained ).

 

The following describes the process of this Code and the meaning of each step. If there are any errors, I hope to correct them.

First, the initinternal () method is displayed. The implementation code of this method is:

Code snippet 1:

/** <Br/> * <p> initialize our internal messageresourcesbundle. </P> <br/> * @ exception servletexception if we cannotinitialize these resources <br/> */<br/> protectedvoidinitinternal () throwsservletexception {</P> <p> //: fixme: Document unavailableexception </P> <p> try {<br/> internal = messageresources. getmessageresources (internalname); <br/>} catch (missingresourceexception e) {<br/> log. error ("cannot load internal resources from '" + internalname + "'", <br/> E ); <br/> throw new unavailableexception <br/> ("cannot load internal resources from '" + internalname + "'"); <br/>}</P> <p>}

 

Code Segment 2:

/** <Br/> * Create and return an instance of <code> messageresources </code> for the <br/> * created by the default <code> messageresourcesfactory </code >. <br/> * @ Param config configuration parameterfor this message bundle. <br/> */<br/> publicsynchronizedstaticmessageresources getmessageresources (string config) {</P> <p> If (defaultfactory = NULL) {<br/> defaultfactory = messageresourcesfactory. createfactory (); <br/>}</P> <p> return defaultfactory. createresources (config); <br/>}

Code snippet 3:

/** <Br/> * Create and return a <code> messageresourcesfactory </code> instance ofthe <br/> * appropriate class, which can be used tocreate customized <br/> * <code> messageresources </code> instances. if no such factory can be <br/> * created, return <code> null </code> instead. <br/> */<br/> publicstaticmessageresourcesfactory createfactory () {</P> <p> // construct a new instance of the specified factory class <br/> try {<br/> If (clazz = NULL) <br/> clazz = requestutils. applicationclass (factoryclass); <br/> messageresourcesfactory factory = <br/> (messageresourcesfactory) clazz. newinstance (); <br/> return (factory); <br/>} catch (throwable t) {<br/> log. error ("messageresourcesfactory. createfactory ", T); <br/> return (null); <br/>}</P> <p>}

The specific function of this method is to initialize messageresources. The specific implementation is the factory mode. First, JudgeDefaultfactoryExistence or not. If not, create a factory,

Defaultfactory= Messageresourcesfactory.Createfactory(), Create a resource class in the factoryDefaultfactory. Createresources (config); If yes, you can directly create a resource class.

The initother () method mainly initializes other configurations and obtains the path of our own Struts-config configuration file,
And its default path is web-INF/struts-config.xml, In addition this method will register some conversion class. The specific source code is:

/** <Br/> * <p> initialize other global characteristics ofthe controller servlet. </P> <br/> * @ exception servletexception if we cannotinitialize these resources <br/> */<br/> protectedvoidinitother () throwsservletexception {</P> <p> string value = NULL; <br/> value = getservletconfig (). getinitparameter ("Config"); <br/> If (value! = NULL) {<br/> Config = value; <br/>}</P> <p> // backwards compatibility for form beans of Java Wrapper Classes <br/> // set to true for strict struts 1.0 compatibility <br/> value = getservletconfig (). getinitparameter ("convertnull"); <br/> If ("true ". equalsignorecase (value) <br/> | "yes ". equalsignorecase (value) <br/> | "on ". equalsignorecase (value) <br/> | "Y ". equalsignorecase (value) <br/> | "1 ". equalsignorecase (value) {</P> <p> convertnull = true; <br/>}</P> <p> If (convertnull) {<br/> convertutils. deregister (); <br/> convertutils. register (New bigdecimalconverter (null), bigdecimal. class); <br/> convertutils. register (New bigintegerconverter (null), biginteger. class); <br/> convertutils. register (New booleanconverter (null), Boolean. class); <br/> convertutils. register (New byteconverter (null), byte. class); <br/> convertutils. register (New characterconverter (null), character. class); <br/> convertutils. register (New doubleconverter (null), double. class); <br/> convertutils. register (New floatconverter (null), float. class); <br/> convertutils. register (New integerconverter (null), integer. class); <br/> convertutils. register (New longconverter (null), long. class); <br/> convertutils. register (new vertex converter (null), short. class); <br/>}</P> <p>}

The initservlet () method uses digester to read the Web. xml file and put it in servletcontext. Specific implementation source code:

/** <Br/> * <p> initialize the Servlet Mapping under which our controller servlet <br/> * is being accessed. this will be used in the <code> & HTML: Form> </code> <br/> * tag to generate correct destination URLs for form submissions. </P> <br/> * @ throws servletexception if error happens while scanning Web. XML <br/> */<br/> protected void initservlet () throws servletexception {</P> <p> // remembe R our servlet name <br/> This. servletname = getservletconfig (). getservletname (); </P> <p> // prepare a digester to scan the web application deployment descriptor <br/> digester = new digester (); <br/> digester. push (this); <br/> digester. setnamespaceaware (true); <br/> digester. setvalidating (false); </P> <p> // register our local copy of The dtds that we can find <br/> for (INT I = 0; I <Regis Trations. length; I + = 2) {<br/> URL url = This. getclass (). getresource (registrations [I + 1]); <br/> If (URL! = NULL) {<br/> digester. register (registrations [I], URL. tostring (); <br/>}</P> <p> // configure the processing rules that we need <br/> digester. addcallmethod ("Web-APP/servlet-mapping", <br/> "addservletmapping", 2); <br/> digester. addcallparam ("Web-APP/servlet-mapping/servlet-name", 0); <br/> digester. addcallparam ("Web-APP/servlet-mapping/url-pattern", 1); </P> <p> // process the Web applic Ation deployment descriptor <br/> If (log. isdebugenabled () {<br/> log. debug ("scanning Web. XML for controller Servlet Mapping "); <br/>}</P> <p> inputstream input = <br/> getservletcontext (). getresourceasstream ("/WEB-INF/web. XML "); </P> <p> If (input = NULL) {<br/> log. error (internal. getmessage ("configwebxml"); <br/> throw new servletexception (internal. getmessage ("configwebxml"); <br/>}</P> <p> Try {<br/> digester. parse (input); </P> <p >}catch (ioexception e) {<br/> log. error (internal. getmessage ("configwebxml"), e); <br/> throw new servletexception (E); </P> <p >}catch (saxexception E) {<br/> log. error (internal. getmessage ("configwebxml"), e); <br/> throw new servletexception (E ); </P> <p >}finally {<br/> try {<br/> input. close (); <br/>}catch (ioexception e) {<br/> log. error (internal. g Etmessage ("configwebxml"), e); <br/> throw new servletexception (E ); <br/>}</P> <p> // record a servlet context attribute (if appropriate) <br/> If (log. isdebugenabled () {<br/> log. debug ("Mapping for servlet '" + servletname + "' = '" + <br/> servletmapping + "'"); <br/>}</P> <p> If (servletmapping! = NULL) {<br/> getservletcontext (). setattribute (globals. servlet_key, servletmapping); <br/>}</P> <p>}

 

This blog first introduces these methods. With the specific functions and implementations of these methods, we gradually know the functions of the init method, and gradually solve the problem when we instantiate actionservlet, digester is how to read/WEB-INF/struts-config.xml file content and put it into actionmapping. Coming soon!

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.