Struts Core code Actionservlet run 2 articles

Source: Internet
Author: User

As early as 3 years ago began to use struts, because it is also a relatively early group of people. Rookie of the time also have seen others write struts of the operation mechanism, also saw part of the code, but are quickie, I understand the general direction of the end. I often ask people if we write action like useraction is not serlvet, is not a singleton. In fact, I have not read the actual code, so when asked others, the heart also often panic, but I am afraid to see from the online information is false.

In the company as a technical manager also a long time, although the company hopes to have a breakthrough in the depth of development, but itself as a small company, the things they involve too much, sometimes to engage in the portal of BEA, sometimes go out blowing bragging, but also often take a few of what will not be the rookie to do projects, these years do not count less, EAI SOA EIP bmp have actual project experience, but which did not insist on all the way down, today leaders want me to do Java time for the company shorter people talk about struts, is long is a chance, you can put struts operating mechanism, take a good look at. Because I think that if we tell everyone hello world, we will not be satisfied. Said a half-day nonsense (I really love to say nonsense) below to enter the topic it.

Struts two functions, my own experience, struts a very useful function is its JSP tag, this is a separate module, our pages are using his tag, although now extremecomponents is also good.

Another major feature of struts is the delivery of the business data stream JSP---->form-"action, which is the core process of the entire struts.

In addition to these two main functions, the rest, are some specific base classes and interfaces, convenient for our own use, from Struts1.0 1.1 1.2 1.3 It is not difficult to see the trend of struts is also more and more simple, more and more use, is not very blindly pursuit of the concept of truth.

In fact, struts operation mechanism, as long as see actionservlet on OK, this afternoon spent 2 hours to see one side, also calculate a little harvest it, the original some of the contents of the speculation to see the source. If you do not understand the singleton and multithreading before, I suggest you add the basic concepts, because all of the 2 concepts in the open source code are core concepts, and the code revolves around these 2 concepts.

Because struts is also run by the JSP servlet mechanism, our entry point begins with the source of the servlet, and we open a struts Web project. Opens the Web. xml file.

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

We found that struts needed to configure a servlet in our project Actionservlet it had several initialization parameters, and a URL containing. Do in the listener URL

We open the Actionservlet class. Take a look at its init () method, because this is the code that executes when you start Tomcat, and see what it did at first.
public void Init () throws Servletexception {

Initinternal ();
Initother ();
Initservlet ();

Initialize modules as needed
Getservletcontext (). SetAttribute (Globals.action_servlet_key, this);
Moduleconfig moduleconfig = Initmoduleconfig ("", config);
Initmodulemessageresources (moduleconfig);
Initmoduledatasources (moduleconfig);
Initmoduleplugins (moduleconfig);
Moduleconfig.freeze ();
Enumeration names = Getservletconfig (). Getinitparameternames ();
while (Names.hasmoreelements ()) {
String name = (string) names.nextelement ();
if (!name.startswith ("config/")) {
Continue
}
String prefix = name.substring (6);
Moduleconfig = Initmoduleconfig
(prefix, Getservletconfig (). Getinitparameter (name));
Initmodulemessageresources (moduleconfig);
Initmoduledatasources (moduleconfig);
Initmoduleplugins (moduleconfig);
Moduleconfig.freeze ();
}
Destroyconfigdigester ();

}

Inside are some initialization operation, actually does not look at the code, we can think out with the foot, it certainly is to put the contents of the Struts-config.xml file into its sealed good configuration information class, as for the configuration information class, I do not introduce, is nothing more than the Factory mode or the Java class of single case,

Let's see initinternal first ()

Internal = messageresources.getmessageresources (InternalName); Instead of looking at it, load the resource file into the class if (defaultfactory = = null)
Defaultfactory = Messageresourcesfactory.createfactory ();
return defaultfactory.createresources (config); Factory mode, which is basically how it works in init.

Initother () This method reads the configuration in Web. xml

<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>

and put this attribute value method actionserlvet protected String config = "/web-inf/struts-config.xml", because the servlet is a singleton, this attribute is taken for granted as a global variable,  It is not a special location, it is not necessary to configure it in Web. XML because this property has a default value. The Initother () method also handles Convertnull, and if you configure it in Web,xml, it will register the default conversion information for Beanutil.

Initservlet () also initializes the information in Web. XML, and puts it in the Getservletcontext (). Does not affect the entire process.

Getservletcontext (). SetAttribute (Globals.action_servlet_key, this); This sentence, very interesting, you register yourself to

Getservletcontext (). In struts, this usage is often seen.

Oduleconfig moduleconfig = Initmoduleconfig ("", config);
Initmodulemessageresources (moduleconfig);
Initmoduledatasources (moduleconfig);
Initmoduleplugins (moduleconfig);
Moduleconfig.freeze ();

Read the contents of the Struts-config.xml file into the class

*************************************************************************************************************** *****

*************************************************************************************************************** The running history of Actionservlet in a struct:

The first is the init () method
================================
1,initinternal ()
Initialize the International Language pack (currently it seems to support only English and Japanese)
2,initother ()
Other initialization
Determine the location of the Struts-config.xml file by reading the value of <param-name>config</param-name> in Web. XML ( Even if the config is not written in Web. XML, there is a default location)
By reading the value of <param-name>convertNull</param-name> in Web. XML to determine if Struts 1.0 is backwards compatible, the default is incompatible
3,initservlet ()
Initializes the Actionservlet mapping and tag library, or reads Web. Xml.
4, save yourself in the ServletContext.
5, initialize the module configuration Factory moduleconfigfactory (most of the struts1.2 new functions, the role of unknown)

Next is the Doget ()/dopost () method
================================
1, for Get and post are handled using the same method
2, by parsing the request to get a path, this path determines which actionmapping to use in the future
3, set internationalization, response contenttype and whether to cache
4, this step is interesting, Actionservlet provides a simple processpreprocess () method that returns a Boolean value, which allows our subclass (Action) to choose whether to overwrite, if the method is overwritten and returns false, The Actionservlet no longer preprocess the request.
5, according to the path generated by the 2nd step to obtain the corresponding configuration information, that is, actionmapping, stored in the request object. If no corresponding information is found, an error message is generated
6, security verification, see the user has no permission to perform this request
7, if Actionform is declared in the re-configuration file, the Actionform instance is created (of course, the creation process for normal actionform and dynamic form is not the same)
8, depending on the scope in the configuration file, the Actionform instance exists in request or session.
9, Next is an exciting scene: Automatically writes data from the request stream to Actionform. Here the struct parses the request stream through a complex set of processes, where the normal form and the "Multipart/form-data" composite form (typically used for uploading files) are processed differently, and finally the data is written through the class map. The reset method of Actionform is called before these operations are performed.
10, if the validation property is set, the validation method of Actionform is called, verifying that if it fails, it attempts to go to the input page, and if no server generates an error page itself
11, get the action instance, where the struct uses a simple caching mechanism that uses a hashmap to hold all action instances (naturally with the class name key), and if the currently needed action is not found in HashMap, instantiate one, Then put in the HashMap.
12, this step is not very clear, it seems that the monitoring configuration file is set forward or include, if set, then go to the corresponding page, and stop the current processing. Why do this, or say forward and include attributes do, or not very name Oh, I hope the expert pointing.
13, finally to this step ... Executes the Execute method of the action instance and obtains the returned forward, hehe.
14, finally jumps to the page that forward points to, if forward is null then does nothing. 、

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.