WebWork2源碼分析續一

來源:互聯網
上載者:User
web
至於Action是的建立則是由ActionProxy來完成的,來看一段簡要的程式調用



ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);



request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());  //調用ActionInvocation



proxy.execute();



其實 ActionProxy是一個介面,而ActionProxyFactory則是一個抽象類別,他們都是通過一個DefaultActionProxy和DefaultActionProxyFactory來完成操作的,且ActionProxy將調用ActionInvocation介面,由DefaultActionInvocation初始化的時候讀取配置,然後由Invoke()方法來完成Action的調用及一些在Action被調用之前的Interceptor的操作.下面是關於DefaultActionInvocation的初始化和調用代碼.



public class DefaultActionInvocation implements ActionInvocation {



private void init() throws Exception {



        Map contextMap = createContextMap();



        createAction();  //載入Action



        if (pushAction) {



            stack.push(action);



        }



        invocationContext = new ActionContext(contextMap);



        invocationContext.setName(proxy.getActionName());



        // get a new List so we don't get problems with the iterator if someone changes the list



        List interceptorList = new ArrayList(proxy.getConfig().getInterceptors()); //擷取配置



        interceptors = interceptorList.iterator();



    }




 


public String invoke() throws Exception {



        if (executed) {



            throw new IllegalStateException("Action has already executed");



        }



//這裡是執行攔截器的操作, 注: 攔截器本身就是AOP的一個特殊實現,Servlet2.3 中Filter就是一個特例啊



        if (interceptors.hasNext()) {



            Interceptor interceptor = (Interceptor) interceptors.next();



            resultCode = interceptor.intercept(this);



        } else {



            resultCode = invokeAction(getAction(), proxy.getConfig());



        }



        // this is needed because the result will be executed, then control will return to the Interceptor, which will



        // return above and flow through again



        if (!executed) {



            if (preResultListeners != null) {



                for (Iterator iterator = preResultListeners.iterator();



                     iterator.hasNext();) {



                    PreResultListener listener = (PreResultListener) iterator.next();



                    listener.beforeResult(this, resultCode);



                }



            }



            // now execute the result, if we're supposed to



            if (proxy.getExecuteResult()) {



                executeResult();



            }



            executed = true;



        }







}



下面再來說說Interceptor 的實現結構,剛開始我以為XWork1.x中Interceptor 應該是從Filter中繼承下來的,後來看了源碼,原來我的想法不對,想想也的確是不需要,也不應該從Filter下繼承,因為Filter就是Servlet2.3的一個API,而XWork1.x設計目的就是要脫離Servlet API,且Interceptor的實現並非是少了Filter就不行,只是我們有了Filter將會來的更加方便!




 


對於WebWork2.x中的所有的攔截器,他們都有一個公用的介面Interceptor,在它當中定義了攔截器的一些基本操作方法,然後有一個AroundInterceptor抽象類別,實現了該介面, AroundInterceptor的作用是組合攔截器的調用順序,代碼如下:



public String intercept(ActionInvocation invocation) throws Exception {



        String result = null;



        before(invocation);  //這裡是用於組合調用順序



        result = invocation.invoke();



        after(invocation, result);



        return result;



  }




 


至於將Map 中的資料轉換到我們的VO中,是通過ParametersInterceptor攔截器來完成操作的,這個攔截器是一個真正的實作類別,他從AroundInterceptor抽象類別下面繼承



public class ParametersInterceptor extends AroundInterceptor {



    //~ Methods ////////////////////////////////////////////////////////////////



    protected void after(ActionInvocation dispatcher, String result) throws Exception {



    }



    protected void before(ActionInvocation invocation) throws Exception {



        if (!(invocation.getAction() instanceof NoParameters)) {



            final Map parameters = ActionContext.getContext().getParameters();



//用於擷取Map 結構中的Parameters



            if (log.isDebugEnabled()) {



                log.debug("Setting params " + parameters);



            }



            ActionContext invocationContext = invocation.getInvocationContext();



            try {



                invocationContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);



                invocationContext.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.TRUE);



                invocationContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);



                if (parameters != null) {



                    final OgnlValueStack stack = ActionContext.getContext().getValueStack();



//用於擷取OgnlValueStack操作,這個package沒看過,具體聽夏昕說是一套可讀寫對象屬性的的類庫,功能有些類似與Jakarta Commons BeanUtils ,及Spring Bean Wrapper



                    for (Iterator iterator = parameters.entrySet().iterator();



//遍曆Parameters中的資訊



                        iterator.hasNext();) {



                        Map.Entry entry = (Map.Entry) iterator.next();



                        String name = entry.getKey().toString();



//填充VO資訊



                        if (acceptableName(name)) {



                            Object value = entry.getValue();



                            stack.setValue(name, value);



                        }



                    }



                }



            } finally {



                invocationContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.FALSE);



                invocationContext.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.FALSE);



                invocationContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.FALSE);



            }



        }



    }



    protected boolean acceptableName(String name) {



        if (name.indexOf('=') != -1 || name.indexOf(',') != -1 || name.indexOf('#') != -1) {



            return false;



        } else {



            return true;



        }



    }



}




相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。