资源描述:
《struts2处理请求的过程》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、struts2处理请求的过程分类:strutsJavaWeb2013-08-2020:21382人阅读评论(0)收藏举报Struts官方的流程图:客户端对tomcat服务器发出请求,将请求封装成HttpRequest对象,并进行预处理操作(如设置编码等);通过web.xml文件找到struts2的前端控制器StrutsPrepareAndExcuteFilter,并调用doFilter()方法。[java]viewplaincopy1.publicvoiddoFilter(ServletRequestreq,ServletResponsere
2、s,FilterChainchain)throwsIOException,ServletException{2.3.HttpServletRequestrequest=(HttpServletRequest)req;4.HttpServletResponseresponse=(HttpServletResponse)res;5.6.try{7.prepare.setEncodingAndLocale(request,response);8.prepare.createActionContext(request,response);9.prep
3、are.assignDispatcherToThread();10.if(excludedPatterns!=null&&prepare.isUrlExcluded(request,excludedPatterns)){11.chain.doFilter(request,response);12.}else{13.request=prepare.wrapRequest(request);14.ActionMappingmapping=prepare.findActionMapping(request,response,true);15.if(
4、mapping==null){16.booleanhandled=execute.executeStaticResourceRequest(request,response);17.if(!handled){18.chain.doFilter(request,response);19.}20.}else{21.execute.executeAction(request,response,mapping);22.}23.}24.}finally{25.prepare.cleanupRequest(request);26.}27.}doFilte
5、r中:1、设置编码;2、创建ActionContext,创建ValueStack对象。3、对请求进行重新封装,根据请求内容的类型不同,返回不同的对象:如果为multipart/form-data类型,则返回MultiPartRequestWrapper类型的对象,否则返回StrutsRequestWrapper类型的对象,MultiPartRequestWrapper是StrutsRequestWrapper的子类,而这两个类都是HttpServletRequest接口的实现。4、根据请求request获取actionMapping对象Act
6、ionMappingmapping=prepare.findActionMapping(request,response,true);如果mapping为null,说明请求的不是Action,会调用execute.executeStaticResourceRequest(request,response);方法,请求静态资源。如果mapping不为null,调用execute.executeAction(request,response,mapping),在这个方法中又调用dispatcher.serviceAction(request,re
7、sponse,servletContext,mapping);方法。[java]viewplaincopy1.publicvoidserviceAction(HttpServletRequestrequest,HttpServletResponseresponse,ServletContextcontext,2.ActionMappingmapping)throwsServletException{3.4.MapextraContext=createContextMap(request,response,mapp
8、ing,context);5.6.//Iftherewasapreviousvaluestack,thencreateanewcopyandpassitintobe