`

webwork源码分析(一)FilterDispatcher和DispatcherUtils

    博客分类:
  • Java
阅读更多

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        String param = filterConfig.getInitParameter("packages");
        String packages = "com.opensymphony.webwork.static template com.opensymphony.webwork.interceptor.debugging";
        if (param != null) {
            packages = param + " " + packages;
        }
        this.pathPrefixes = parse(packages);
        DispatcherUtils.initialize(filterConfig.getServletContext());
    }

ps:

DispatcherUtils进行了初始化,参数为servletcontext,采用 单例模式。

初始化方法中对packages进行了parse,目前我还不知道在哪需要用到

DispatcherUtils.initialize(filterConfig.getServletContext());


方法中进行了xwork的一些处理 text的loacl处理  ObjectFactory的初始化处理

webwork 的Configuration 的处理

总而言之,这一句简简单单的initialize方法,让我们根据web.xml中得到的基本信息中初始化了

application级别的相关东西,xwork所需要的全局的东西也得到初始化,webwork 下的config目录也发挥了他的作用

 

接下来,我们看看dofilter 这个起核心作用的方法

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {


        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        ServletContext servletContext = filterConfig.getServletContext();

        // prepare the request no matter what - this ensures that the proper character encoding
        // is used before invoking the mapper (see WW-9127)


        DispatcherUtils du = DispatcherUtils.getInstance();


        du.prepare(request, response);//正如这个方法名字一样进行locale、encoding以及特殊request parameters设置


        try {
         // Wrap request first, just in case it is multipart/form-data
         // parameters might not be accessible through before encoding (ww-1278)
            request = du.wrapRequest(request, servletContext);//对request进行包装
        } catch (IOException e) {
            String message = "Could not wrap servlet request with MultipartRequestWrapper!";
            LOG.error(message, e);
            throw new ServletException(message, e);
        }
       

        ActionMapperIF mapper = ActionMapperFactory.getMapper();//得到action的mapper
        ActionMapping mapping = mapper.getMapping(request);// 得到action 的 mapping

        if (mapping == null) {
            // there is no action in this request, should we look for a static resource?
            String resourcePath = RequestUtils.getServletPath(request);

            if ("".equals(resourcePath) && null != request.getPathInfo()) {
                resourcePath = request.getPathInfo();
            }

            if ("true".equals(Configuration.get(WebWorkConstants.WEBWORK_SERVE_STATIC_CONTENT))
                    && resourcePath.startsWith("/webwork")) {
                String name = resourcePath.substring("/webwork".length());
                findStaticResource(name, response);
            } else {
                // this is a normal request, let it pass through
                chain.doFilter(request, response);
            }
            // WW did its job here
            return;
        }


        Object o = null;
        try {

            //setupContainer(request);

            o = beforeActionInvocation(request, servletContext);

//最核心的方法,这里通过ActionProxyFactory生成ActionProxy(包含actioninvocation actioncontext的信息供拦截器配合result一起发挥ww和xwork的作用)

            du.serviceAction(request, response, servletContext, mapping);


        } finally {
            afterActionInvocation(request, servletContext, o);
            ActionContext.setContext(null);
        }
    }

 

这里注意几个factory的使用

1、ActionMapperFactory

        ActionMapperIF mapper = ActionMapperFactory.getMapper();
        ActionMapping mapping = mapper.getMapping(request);

2、ActionProxyFactory

ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);
得到该action的代理实例

特别要注意的actioninvocation也是在proxy生成的时候初始化好了,

proxy包含了 action所需要的所有东西,完成接下来的一系列工作,

感觉其实这些东西也是比较简单的,通过传引用解决了哪需要就设置到哪去这种方式,不用担心对象的内容改变。



du.serviceAction(request, response, servletContext, mapping);该方法如下:

 public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
        Map extraContext = createContextMap(request, response, mapping, context);

        // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
        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(XWorkContinuationConfig.CONTINUE_PARAM);
            if (id != null) {
                // remove the continue key from the params - we don't want to bother setting
                // on the value stack since we know it won't work. Besides, this breaks devMode!
                Map params = (Map) extraContext.get(ActionContext.PARAMETERS);
                params.remove(XWorkContinuationConfig.CONTINUE_PARAM);

                // and now put the key in the context to be picked up later by XWork
                extraContext.put(XWorkContinuationConfig.CONTINUE_KEY, id);
            }

            ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);
            proxy.setMethod(method);
            request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());

            // 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 (ConfigurationException e) {
            LOG.error("Could not find action", e);
            sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);
        } catch (Exception e) {
            String msg = "Could not execute action";
            LOG.error(msg, e);
            sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
        }
    }


ps:webwork 其实是一个很简单的框架,实现servlet协议,包装httpreques\httpresponse\httpsession\\httpservletcontext进行解偶,通过xwork来处理核心逻辑,不过感觉其包结构不够好,框架虽简单新手入门却不容易。
 

 


 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics