Struts2源碼分析(一) 一一一 ModelDrivenInterceptor

			ModelDrivenInterceptor的核心源碼

【說明】 ModelDrivenInterceptor是默認的攔截器鏈(struts-default)的一部分,當一個請求經過ModelDrivenInterceptor的時候,它會判斷當前要調用的Action對象是否實現了ModelDriven接口,	如果實現了這個接口,則調用getModel()方法,並把返回值壓入ValueStack。

// 核心代碼:
public class ModelDrivenInterceptor extends AbstractInterceptor {

	@Override
    	public String intercept(ActionInvocation invocation) throws Exception {
        	Object action = invocation.getAction();
		
		// 如果action實現了ModelDriven接口,則執行下面的代碼
        	if (action instanceof ModelDriven) {
			// 把action強轉爲ModelDriven
           	 	ModelDriven modelDriven = (ModelDriven) action;
			// 獲得值棧
            		ValueStack stack = invocation.getStack();
			// 獲得model
            		Object model = modelDriven.getModel();
            		if (model !=  null) {
				// ***** 把獲得的model壓入ValueStack的對象棧(Value Stack Contents)中 *****
            			stack.push(model);
            		}
        	}
        	return invocation.invoke();
    	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章