资源描述:
《struts2处理请求的过程.docx》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、struts2处理请求的过程分类: struts JavaWeb2013-08-2020:21 382人阅读 评论(0) 收藏 举报Struts官方的流程图:客户端对tomcat服务器发出请求,将请求封装成HttpRequest对象,并进行预处理操作(如设置编码等);通过web.xml文件找到struts2的前端控制器StrutsPrepareAndExcuteFilter,并调用doFilter()方法。[java] viewplaincopy1. public void doFilter(ServletRequest req, Se
2、rvletResponse res, FilterChain chain) throws IOException, ServletException { 2. 3. HttpServletRequest request = (HttpServletRequest) req; 4. HttpServletResponse response = (HttpServletResponse) res; 5. 6. try { 7. prepare.setEncodingAndLocale(re
3、quest, response); 1. prepare.createActionContext(request, response); 2. prepare.assignDispatcherToThread(); 3.if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) { 4. chain.doFilter(request, response); 5.} else { 6.
4、 request = prepare.wrapRequest(request); 7. ActionMapping mapping = prepare.findActionMapping(request, response, true); 8. if (mapping == null) { 9. boolean handled = execute.executeStaticResourceRequest(request, response); 10. if (!handled) {
5、 11. chain.doFilter(request, response); 12. } 13. } else { 14. execute.executeAction(request, response, mapping); 15. } 16.} 17. } finally { 18. prepare.cleanupRequest(request); 19. } 20. } doFilter中:1、设置编码;2、创建Act
6、ionContext,创建ValueStack对象。3、对请求进行重新封装,根据请求内容的类型不同,返回不同的对象:如果为multipart/form-data类型,则返回MultiPartRequestWrapper类型的对象,否则返回StrutsRequestWrapper类型的对象,MultiPartRequestWrapper是StrutsRequestWrapper的子类,而这两个类都是HttpServletRequest接口的实现。4、根据请求request获取actionMapping对象ActionMappingmapp
7、ing=prepare.findActionMapping(request,response,true);如果mapping为null,说明请求的不是Action,会调用execute.executeStaticResourceRequest(request,response);方法,请求静态资源。如果mapping不为null,调用execute.executeAction(request,response,mapping),在这个方法中又调用dispatcher.serviceAction(request,response,serv
8、letContext,mapping);方法。[java] viewplaincopy1.public void serviceAction(HttpServletRequest request, Http