struts2处理请求的过程资料

上传人:f****u 文档编号:128295385 上传时间:2020-04-20 格式:PDF 页数:9 大小:254.16KB
返回 下载 相关 举报
struts2处理请求的过程资料_第1页
第1页 / 共9页
struts2处理请求的过程资料_第2页
第2页 / 共9页
struts2处理请求的过程资料_第3页
第3页 / 共9页
struts2处理请求的过程资料_第4页
第4页 / 共9页
struts2处理请求的过程资料_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《struts2处理请求的过程资料》由会员分享,可在线阅读,更多相关《struts2处理请求的过程资料(9页珍藏版)》请在金锄头文库上搜索。

1、struts2 处理请求的过程 分类 struts JavaWeb2013 08 20 20 21 382 人阅读 评论 0 收藏 举报 Struts 官方的流程图 客户端对 tomcat 服务器发出请求 将请求封装成 HttpRequest 对象 并进行预处理操作 如设置编码等 通过 web xml 文件 找到 struts2 的前端控制器 StrutsPrepareAndExcuteFilter 并调用 doFilter 方法 java view plaincopy 1 public void doFilter ServletRequest req ServletResponse res

2、FilterChain c hain throws IOException ServletException 2 3 HttpServletRequest request HttpServletRequest req 4 HttpServletResponse response HttpServletResponse res 5 6 try 7 prepare setEncodingAndLocale request response 8 prepare createActionContext request response 9 prepare assignDispatcherToThrea

3、d 10 if excludedPatterns null 12 else 13 request prepare wrapRequest request 14 ActionMapping mapping prepare findActionMapping request response tru e 15 if mapping null 16 boolean handled execute executeStaticResourceRequest request resp onse 17 if handled 18 chain doFilter request response 19 20 e

4、lse 21 execute executeAction request response mapping 22 23 24 finally 25 prepare cleanupRequest request 26 27 doFilter 中 1 设置编码 2 创建 ActionContext 创建 ValueStack 对象 3 对请求进行重新封装 根据请求内容的类型不同 返回不同的对象 如果为 multipart form data 类型 则返回 MultiPartRequestWrapper 类 型的对象 否则返回 StrutsRequestWrapper 类型的对象 MultiPart

5、RequestWrapper 是 StrutsRequestWrapper 的子类 而这两 个类都是 HttpServletRequest 接口的实现 4 根据请求 request 获取 actionMapping 对象 ActionMapping mapping prepare findActionMapping request response true 如果 mapping 为 null 说明请求的不是 Action 会调用 execute executeStaticResourceRequest request response 方法 请 求静态资源 如果 mapping 不为 nul

6、l 调用 execute executeAction request response mapping 在这个方法中又调用 dispatcher serviceAction request response servletContext mapping 方法 java view plaincopy 1 public void serviceAction HttpServletRequest request HttpServletResponse re sponse ServletContext context 2 ActionMapping mapping throws ServletExce

7、ption 3 4 Map extraContext createContextMap request response m apping context 5 6 If there was a previous value stack then create a new copy and pass it in to be used by the new Action 7 ValueStack stack ValueStack request getAttribute ServletActionContex t STRUTS VALUESTACK KEY 8 boolean nullStack

8、stack null 9 if nullStack 10 ActionContext ctx ActionContext getContext 11 if ctx null 12 stack ctx getValueStack 13 14 15 if stack null 16 extraContext put ActionContext VALUE STACK valueStackFactory create ValueStack stack 17 18 19 String timerKey Handling request from Dispatcher 20 try 21 UtilTim

9、erStack push timerKey 22 String namespace mapping getNamespace 23 String name mapping getName 24 String method mapping getMethod 25 26 Configuration config configurationManager getConfiguration 27 ActionProxy proxy config getContainer getInstance ActionProxyFac tory class createActionProxy 28 namesp

10、ace name method extraContext true false 29 30 request setAttribute ServletActionContext STRUTS VALUESTACK KEY pro xy getInvocation getStack 31 32 if the ActionMapping says to go straight to a result do it 33 if mapping getResult null 34 Result result mapping getResult 35 result execute proxy getInvo

11、cation 36 else 37 proxy execute 38 39 40 If there was a previous value stack then set it back onto the req uest 41 if nullStack 42 request setAttribute ServletActionContext STRUTS VALUESTACK KEY stack 43 44 catch ConfigurationException e 45 WW 2874 Only log error if in devMode 46 if devMode 47 Strin

12、g reqStr request getRequestURI 48 if request getQueryString null 49 reqStr reqStr request getQueryString 50 51 LOG error Could not find action or result n reqStr e 52 53 else 54 LOG warn Could not find action or result e 55 56 sendError request response context HttpServletResponse SC NOT FOU ND e 57

13、 catch Exception e 58 sendError request response context HttpServletResponse SC INTERNA L SERVER ERROR e 59 finally 60 UtilTimerStack pop timerKey 61 62 在这个 serviceAction 方法中 1 将相关对象信息封装为 Map 如 HttpServletRequest Http parameters HttpServletResponse HttpSession ServletContext ActionMapping 等对象信息 存入到执

14、行上下文 Map 中 返回执行 上下文 Map 对象 extraMap 2 获取 ValueStack 对象 并放入 map 中 3 获取 mapping 对象中存储的 action 命名空间 name 属性 method 属性等信息 4 加载并解析 Struts2 配置文件 如果没有人为配置 默认按顺序加载 struts default xml struts plugin xml struts xml 将 action 配置 result 配置 interceptor 配置 解析并存入至 config 对象中 返回文件配置对 象 config 5 根据执行上下文 Map action 命名空

15、间 name 属性 method 属 性等创建创建 ActionProxy 对象对象 createActionProxy 方法中 5 1 创建 invacation ActionInvocation inv new DefaultActionInvocation extraContext true 5 2 创建 proxy DefaultActionProxy proxy new DefaultActionProxy inv namespace actionName methodName executeResult cleanupContext 5 3 接着 proxy prepare 方法中

16、 5 4 初始化 action invocation init this 方法中 使用反射创建 action 并压入值栈栈顶 准备拦截器集合 接着 6 执行 ActionProxy 对象的 proxy execute 方法 并转向结果 java view plaincopy 1 public String execute throws Exception 2 ActionContext previous ActionContext getContext 3 ActionContext setContext invocation getInvocationContext 4 try 5 This is for the new API 6 return RequestContextImpl callInContext invocation new Callab le 7 public String call throws Exception 8 return invocation invoke 9 10 11 12 return invocation invoke 13 finally 1

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号