Struts central controller

Source: Internet
Author: User
In the Struts framework, multiple components share the work of controllers, including actionservlet, requestprocessor, and action. actionservlet is the core controller in the Struts framework.
Struts Application Program General process of processing user requests
As the central servlet, The actionservlet class handles all incoming user requests. When the actionservlet receives a user request, it will execute the following process:
1. Retrieve the actionmapping instance that matches the user request. If it does not exist, the system returns an invalid error message from the user request path.
2. If form bean is configured for actionmapping, The actionform instance within the corresponding range is obtained. If the actionform instance does not exist, create an actionform object and save the form data submitted by the customer to the actionform object.
3. Determine whether to verify the form based on the actionmapping configuration information. If verification is required, call the validate () method of actionform.
4. If the validate () method of actionform returns NULL, or an actionerrors object that does not contain any actionmessage is returned, the form verification is successful and the next step is continued. Otherwise, the page specified by the INPUT attribute is returned, and the error information in the actionerrors collection is displayed to the user.
5. actionservlet determines the action to which the request is forwarded based on the ing information contained in the actionmapping instance. If the corresponding action instance does not exist, create the instance and then call the execute method of the action.
6. The execute method of action returns an actionforward object. actionservlet forwards the request to the component specified by this actionforward object, usually a JSP component or other actions.
7. If the actionforward object points to a JSP component, the dynamic page generated by the JSP is returned to the user. If the actionforward object still points to an action, the execution starts again in step 1!

From the above process, we can see that the actionservlet class is the built-in core controller component of the Struts framework. It inherits the javax. servlet. http. httpservlet class. Struts usually starts from loading actionservlet. The Web container loads the actionservlet when the first request of the struts application is started or the first request arrives.
After the actionservlet is loaded for the first time, its Init () method is called. In the init () method, the Struts framework executes all necessary initialization work.
Then, the Web Container will call the dopost () or doget () Methods of actionservlet to process user requests. In fact, they all call the process () method to process specific requests, as shown in the following Code :
Actionservlet processes user requests:

// From org. Apache. Struts. Action. actionservlet.

  Public   Void Doget (httpservletrequest request, httpservletresponse response)
Throws Ioexception, servletexception {
Process (request, response );
}

Public   Void Dopost (httpservletrequest request, httpservletresponse response)
Throws Ioexception, servletexception {
Process (request, response );
}

Protected   Void Process (httpservletrequest request, httpservletresponse response)
Throws Ioexception, servletexception {
// Select an appropriate application module based on the request
Moduleutils. getinstance (). selectmodule (request, This . Getservletcontext ());
Moduleconfig config = Getmoduleconfig (request );
// Obtain the requestprocessor instance related to the module
Requestprocessor Processor = Getprocessorformodule (config );
If (Processor =   Null )
Processor = Getrequestprocessor (config );
// Call the process () method of the processor instance to process the request.
Processor. Process (request, response );
}

As shown above, the process () method of the requestprocessor class is used to actually process user requests in the actionservlet process () method. The Struts framework only allows the application to have one actionservlet class, but each application module has its own requestprocessor class instance. In the process () method of actionservlet, once the correct application module is selected, the process () method of the corresponding module requestprocessor instance is called to process the request. The process method of the requestprocessor class is as follows:

// From requestprocessor
Public   Void Process (httpservletrequest request, httpservletresponse response) Throws Ioexception, servletexception
{
// Encapsulate muiltipart requests with a special packaging object
Request = Processmultipart (request );
// Verify the path component we used to select the ing
String path = Processpath (request, response );

If (Path ! =   Null )
{
If (Log. isdebugenabled ())
Log. debug ( " Processing' "   + Request. getmethod () +   " 'For Path' "   + Path +   " ' " );
// When there is a request, select a locale object for the current user
Processlocale (request, response );
// When there is a request, set the content type and no-caching Header
Processcontent (request, response );
Processnocache (request, response );
If (Processpreprocess (request, response ))
{
Processcachedmessages (request, response );
// Verify the request ing
Actionmapping Mapping = Processmapping (request, response, PATH );
If (Mapping ! =   Null   && Processroles (request, response, mapping ))
{
// Process any actionform bean related to this request
Actionform form = Processactionform (request, response, Mapping );
Processpopulate (request, response, form, Mapping );
If (Processvalidate (request, response, form, mapping) && Processforward (request, response, mapping) && Processinclude (request, response, mapping ))
{
// Create or obtain an action instance to process this request
Action action = Processactioncreate (request, response, Mapping );
If (Action ! =   Null )
{
// Call the action instance itself
Actionforward forward = Processactionperform (request, response, action, form, Mapping );
// Process the returned actionforward instance
Processforwardconfig (request, response, forward );
}
}
}
}
}
}

Configuration of actionservlet in Web. xml:
Like all Servlets, actionservlet also needs to be configured in Web. xml. The specific configuration method is as follows:

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

It should be particularly noted that the struts configuration file struts-config.xml is declared as an initialization parameter value of actionservlet in the web. xml file. In addition, the URL format of actionservlet is *. Do. In this way, when any URL suffix *. Do is entered in the browser address, the system maps to actionservlet.

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.