controller components of struts

Controller components of struts mainly include:

    ActionServlet: as a center controller of struts framework.

    RequestProcessor:as the request processor of every sub applicaiton model.

    Action: handle a detailed transaction

main mission of them is:

    1.receive the request of user.

    2.execute relevant transaction logic by calling proper model compnent according to the request of user.

    3.get the execution result of relevant transaction logic

    4.choose the  proper view component to user according to the execution result and the present situtation.

----------------------------------------------------------------------------------------------------------------------------------

ActionServlet:

     There is just one instance of Actionservlet in the lifecycle.but can response many user request.

     When launch the servlet container or send a request to the actionServlet for the first time to load the ActionServlet,in both these situations,servlet container will execute the init() method of ActionServlet immediately after the ActionServlet loaded to make sure that it have been initialized when handling the user request.

     Init method have a flow:

     1.call initInternal() method, in order to initialize the internal message resources like:notice,alert,error message which related to system log.

     2.call initOther() method, in order to initialize the initial parameter in web.xml when load this servlet.like config,debug,detail and so on

     3.call initServlet() method,load the URL mapping information of Actionservlet from web.xml,also can register the DTD files to validate the grammer of web.xml and struts-config.xml.

     4.call initModuleConfig() method,load and analysis the default configuration files of sub application.Create the ModuleConfig object and store it to the ServletContext.

     5.call initModuleMessageResources() method,load and initialize the message resource of the defalut sub application.Create the MessageResourcesobject and store it to the ServletContext.

     6.call initModuleDataResources() method,load and initialize the data resources of the defalut sub application.This step will be cancelled if the <data-sources> element doesnot define in struts-config.xml

     7.call initModulePlugins(),load and initialize all the plugins of the defalut sub application.

     8.After the default application been successfully initialized,if there are some other sub applications,the flow will loop step 4 to step 7.to initialize other application separately.

     When ActionServlet instance receive the HTTP request,it will call process() method of doGet or doPost to handle with this request.source code of method process in ActionServlet.

protected void process(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{

     ModuleUtils.getInstance().selectModule(request.getServletContext);
    getRequestProcessor(getModuleConfig(request)).process(request,response);//go to RequestProcessor
}

 

----------------------------------------------------------------------------------------------------------------------------------

From the source code of ActionSevlet,we can get that process method resend to RequestProcessor to deal with.ok,let's check what happened in process() method of RequestProcessor :

   1.call processMultipart() method,if the HTTP request type is POST and contentType is start with "multipart/form-data",normal HttpServletRequest object will be repackaged for deal with the HTTP request of "multipart" type easily.If the HTTP request type is GET or the contentType is not "multipart",just returen the original HttpServletRequset Object.

   2.call processPath() method,to achieve the URL path of request.this information can use for choosing the proper struts action component.

   3.call processLocale() method,when the locale property of ControllerConfig is true,get the local information included in user request,then save the locale instance into session scope.

   4.call processContent() method.get the contentType property of ControllerConfig,then set the document type and charset by calling response.setContentType(contentType).

   5.call processNoCache() method,get the nocache propety of ControllerConfig,if the value is true,some specail head parameters like Pragma,Cache-Control and expires will be add into response result,in order to avoid the page store into the cache of client browser.

   6.call processPreprocess() method,sub class can override this method to execute the pre-process request operation.

   7.call processMapping() method,find the ActionMapping matching with request URL.if not exist,return error message.

   8.call processRoles() method,judge if the security roles setted or not,if did,judge the current user has the neccssary roles by method isUserInRole(),else end the process flow and return error message.

   9.call processActionForm() method,judget the ActionForm defined or not for ActionMapping firstly,if did,find ActionForm instance in ActionForm active scope.if not,create an instance,then save it in the proper scope with the key is the name property of ActionMapping.

   10.call processPopulate() method,if the ActionForm has already been configured in ActionMapping,should call the reset() method of ActionForm,then put the form resource into ActionForm.

   11.call processValidate() method,if the ActionForm has already been configured in ActionMapping,and the value of validate property is true,method validate() will be called.If the ActionErrors returned by this method including the ActionMessage,that means the form validation is failed,store the ActionErrors into the request scope,and redirect to the Web component pointed by input property of ActionMapping.it the validation is success,go to next step of this flow.

   12.call processForward() method,judge if the forward property setted in ActionMapping or not,if did,call forward() method of RequestDispatcher,request handling flow is over. or go to next step.

   13.call processInclude() method,judge if the include property setted in ActionMapping or not,if did,call include() method of RequestDispatcher,request handling flow is over. or go to next step.

   14.call processActionCreate() method,firstly,check if the Action instance exist in Action cache,if not create a new Action instance,then save it in Action cache.

   15.call processActionPerform() method,this method will call the execute() method of Action instance.this method surround by try/catch for catch the exception easily.

   16.call processActionForward() method,transfer the ActionForward returned by exectue method of Action to Action.processActionForward() execute the redirect and send accroding to the request information of ActionForward.

----------------------------------------------------------------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值