虽然这三个参数都可以进行设置,但是在defaultStack中struts2并没有为其赋值,所以初始值这三个属性都为null。
下面是ScopedModelDrivenInterceptor的intercept方法源码:
@Override public String intercept(ActionInvocation invocation) throws Exception { Object action = invocation.getAction();//获取当前正在执行的Action //如果Action实现了ScopedModelDriven接口 if (action instanceof ScopedModelDriven) { ScopedModelDriven modelDriven = (ScopedModelDriven) action;//强转 if (modelDriven.getModel() == null) {//如果返回为null,如果不为null的话是不会去修改该Action中model属性的 ActionContext ctx = ActionContext.getContext();//获取ActionContext ActionConfig config = invocation.getProxy().getConfig();//获取Action配置 //model类名,初始值为null String cName = className; if (cName == null) { try { Method method = action.getClass().getMethod(GET_MODEL, EMPTY_CLASS_ARRAY);//通过反射获取getModel方法 Class cls = method.getReturnType();//获取getModel方法的返回值类型 cName = cls.getName();//获取model类名 } catch (NoSuchMethodException e) {//如果没有getModel方法则抛异常 throw new XWorkException("The " + GET_MODEL + "() is not defined in action " + action.getClass() + "", config); } } String modelName = name;//model存储的key,初始值为null if (modelName == null) { modelName = cName;//把model的类名赋给model存储的key } Object model = resolveModel(objectFactory, ctx, cName, scope, modelName);//解析model modelDriven.setModel(model);//把返回的model对象设置到Action中 modelDriven.setScopeKey(modelName);//返回model存储的key设置到Action中 } } return invocation.invoke();//调用下一个拦截器 }
protected Object resolveModel(ObjectFactory factory, ActionContext actionContext, String modelClassName, String modelScope, String modelName) throws Exception { Object model = null; Map<String, Object> scopeMap = actionContext.getContextMap();//获取ActionContext内容的context Map对象 if ("session".equals(modelScope)) {//如果配置了scope为session,但这里并没有配置 scopeMap = actionContext.getSession();//所以这里不会执行 } model = scopeMap.get(modelName);//去context Map(即相当于request作用域)中查找 if (model == null) {//没有找到 model = factory.buildBean(modelClassName, null);//调用对象工厂的buildBean方法实例化一个对象 scopeMap.put(modelName, model);//放置到context Map中 } return model;//返回model对象 }