作者: gkm422  链接:http://remind.javaeye.com/blog/203487  发表时间: 2006年11月28日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

      WebWork发展到现在已经到了2.X版本,因为接触的晚,所以一开始就用上了WebWork2的版本,据说WebWork2.x+XWork1.x 的架构分离了与Servlet API的紧偶合,使系统结构更加清晰,因为工作的关系,我开始用上了WebWork2,现在就用的过程做一个总结,再把WebWork2过上一遍,以便我更深入的学习WebWork2。(本文参考了夏昕的<<Webwork2_Guide>>,以及CSDN上阿明的专栏,并非原创,只是自己写出来参考学习!)
     
在新的架构中WebWork2.x的作用是负责将用户的HTTP请求分离出来,使的请求完全的脱离Servlet API,然后将这些请求用Map的方式传入XWork1.x,且XWork1.x通过Interceptor 将Map中的数据传到我们的VO中,然后由对应的自定义Action来调用.

       首先打开DispatcherUtils.java,它在com.opensymphony.webwork.dispatcher包下面。

       找到serviceAction这个方法,它实例化了Map请求,从请求的服务中解析出对应的Action名称。
       HashMap extraContext = createContextMap(requestMap, params, session, application, request, response, context);
       OgnlValueStack stack = (OgnlValueStack) request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY);
        if (stack != null) {
            extraContext.put(ActionContext.VALUE_STACK, new OgnlValueStack(stack));
        }
       try {
            String namespace = mapping.getNamespace();
            String name = mapping.getName();
            String method = mapping.getMethod();

            String id = request.getParameter("continue");
            if (id != null) {
                extraContext.put("__continue", id);
            }
       // if the ActionMapping says to go straight to a result, do it!
            if (mapping.getResult() != null) {
                Result result = mapping.getResult();
                result.execute(proxy.getInvocation());
            } else {
                proxy.execute();
            }

            // If there was a previous value stack then set it back onto the request
            if (stack != null) {
                request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, stack);
            }
        } catch 。。。。。。。
       找到createContextMap这个方法,它遍历了HttpServletRequest,HttpSession,ServletContext中的数据,将他们复制到Map中从而内部结构与Servlet API相分离。
         public HashMap createContextMap(Map requestMap,
                                    Map parameterMap,
                                    Map sessionMap,
                                    Map applicationMap,
                                    HttpServletRequest request,
                                    HttpServletResponse response,
                                    ServletContext servletContext) {
        HashMap extraContext = new HashMap();
//存放HTTP中上传文件的request  session请求
        extraContext.put(ActionContext.PARAMETERS, new HashMap(parameterMap));
        extraContext.put(ActionContext.SESSION, sessionMap);
        extraContext.put(ActionContext.APPLICATION, applicationMap);
        extraContext.put(ActionContext.LOCALE, (locale == null) ? request.getLocale() : locale);
        extraContext.put(ActionContext.DEV_MODE, Boolean.valueOf(devMode));

        extraContext.put(WebWorkStatics.HTTP_REQUEST, request);
        extraContext.put(WebWorkStatics.HTTP_RESPONSE, response);
        extraContext.put(WebWorkStatics.SERVLET_CONTEXT, servletContext);
        extraContext.put(ComponentInterceptor.COMPONENT_MANAGER, request.getAttribute(ComponentManager.COMPONENT_MANAGER_KEY));

        // 这里用于存放HTTP中的request  session请求
        extraContext.put("request", requestMap);
        extraContext.put("session", sessionMap);
        extraContext.put("application", applicationMap);
        extraContext.put("parameters", parameterMap);

        AttributeMap attrMap = new AttributeMap(extraContext);
        extraContext.put("attr", attrMap);

        return extraContext;
    }
       创建Action,由ActionProxy来完成。
            ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext);
            proxy.setMethod(method);
            request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());


本文的讨论也很精彩,浏览讨论>>


JavaEye推荐




文章来源:http://remind.javaeye.com/blog/203487