Struts-three major components (2) requestprocessor

Source: Internet
Author: User


Actionservlet is the only servlet in the Struts framework that processes all requests. Whenever it receives a request, it first tries to find a sub-Application for the existing request. Once the sub-application is found, it will generate a requestprocessor object for it and call the process () method that passes in httpservletrequest and httpservletresponse as parameters.


1. requestprocessor. Process ()

Java code:

Public void process (httpservletrequest request, httpservletresponse response) throws ioexception, servletexception {// If httpservletrequest is post, the request is a multipart/form-data // Struts framework that wraps the request object into a request object dedicated to processing multipart requests. Otherwise, simply return the original request Object Request = processmultipart (request); // This method is used to obtain the application path from the request URI. The obtained information is used to select an appropriate struts action call in a later step. String Path = processpath (request, response); If (Path = NULL) {return;} If (log. isdebugenabled () {log. debug ("processing a'" + request. getmethod () + "'For path'" + path + "'");} // process some international transactions processlocale (request, response ); // determine the request's content type encoding (encoding) method processcontent (request, response); // call the processnocache () method based on the settings of the nocache attribute, // If nocache is set to true. Add the appropriate response header to the response object so that the page is retained in the browser cache processnocache (request, response ); // general purpose preprocessing hook // This method sets a hook here, the default Implementation of the method is simply to return true, // This provides developers with custom processors with a suitable place for you to add your own business logic. // This method is called before the action is called. If you reload this method and only need to return false, the action will not be called if (! Processpreprocess (request, response) {return;} This. processcachedmessages (request, response); // identify the mapping for this request // determines whether to return the actionmapping object instance based on the path information in the customer request information. // If the path ing cannot be found, the customer will receive an error response. Actionmapping mapping = processmapping (request, response, PATH); If (mapping = NULL) {return ;} // check for any role required to perform this action // check whether a security role is configured for the action. // If role requirements are configured, The isuserinrole () method of the request object is called. // if the user belongs to these roles, the customer will receive an error response. If (! Processroles (request, response, mapping) {return;} // process any actionform bean related to this request // check whether there is an actionform configured for actionmapping. // If an actionform instance exists, search for whether the actionform instance exists in the valid region. If so, the instance is reused. If the actionform instance does not exist, an instance is created. // Save the instance and the configured valid region (request, session, application) in the configuration file, // use the name attribute of the Action element as the keyword of the instance. Actionform form = processactionform (request, response, Mapping); // if there is an actionform configured for actionmapping, // encapsulate the data in the request object to actionform, // call the reset () method of actionform to set the default value before encapsulation. Processpopulate (request, response, form, Mapping); // validate any fields of the actionform bean, if applicable // If actionform is configured, in addition, the property validate of the action element is set to true, // The Validate () method is further called for rule validation. // If the validation of the validate () method fails, an actionerrors object will be saved to the request area. // The request will be automatically redirected to the page specified by the INPUT attribute mapped to the action. // If the verification is passed or the actionform is not configured in the Action ing, the request will continue to be processed. Try {If (! Processvalidate (request, response, form, mapping) {return ;}} catch (invalidcancelexception e) {actionforward forward = processexception (request, response, E, form, Mapping ); processforwardconfig (request, response, forward); return;} catch (ioexception e) {Throw E;} catch (servletexception e) {Throw E ;} // determine the next operation based on whether the forward or include attribute is configured for the action ing. // If any one is configured, call the forward () or include () method of the requestdispatcher object accordingly. // after the call, the processing of the customer's request ends. Otherwise, process the request. // Process a forward or include specified by this mappingif (! Processforward (request, response, mapping) {return;} If (! Processinclude (request, response, mapping) {return;} // create or obtain an action object instance to process the request. // The processactioncreate () method searches for existing action instances in the cache. // If yes, It is reused. Otherwise, it is re-created and stored in the cache. Action action = processactioncreate (request, response, Mapping); If (Action = NULL) {return ;} // call the action instance itself // This method is used to call the action instance's execute () method in a try/catch code block. // This ensures that the action's execute () method can be captured by requestprocessor if an execution exception occurs. Actionforward forward = processactionperform (request, response, action, form, Mapping); // process the returned actionforward instance // check the actionforward object instance and decide to use the redirect or forword Method for redirection. Whether redirect or // forword is used depends on the Redirect attribute value of the forward element. Processforwardconfig (request, response, forward );}

Ii. Extended requestprocessor class

If you want to modify some default functions of requestprocessor, you can easily override the related methods in the requestprocessor base class.

Java code

Public writable extends requestprocessor {protected void processpreprocess (httpservletrequest request, httpservletresponse response) {request. setcharacterencoding ("UTF-8"); // Add a line here to set the encoding generation. Super. Process (request, response );}}


In the struts configuration file, the processorclass attribute of the <controller> element is used to configure your own requestprocessor class.

<controller  contentType=“text/html:charset=”GB2312”  locale=”true”nocache=”true” processorCalss=”com.test.CustomRequestProcessor”/> 



Most of the requests are processed in requestprocessor. Process. The process () method is implemented in the design mode of the template method (templatemethod), where there are methods for completing each step of request processing; all these methods are called sequentially from the process () method. For example, there are several separate methods to search for the actionform class of the current request and check whether the current user has the permission to execute actionmapping. This provides us with great flexibility. Struts's requestprocessor provides the default implementation method for each request processing step. In this way, you can rewrite the required method, while the remaining methods are retained by default.

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