1、ActionProxy是Action的一个代理类,也就是说Action的调用是通过ActionProxy实现的,其实就是调用了ActionProxy.execute()方法,而该方法又调用了ActionInvocation.invoke()方法,上图中的StrutsActionProxy类实现类ActionProxy代理类。
<interceptor-stack name="defaultStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="i18n"/> <interceptor-ref name="prepare"/> <interceptor-ref name="chain"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="multiselect"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"> <param name="excludeParams">^action:.*,^method:.*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="debugging"/> <interceptor-ref name="deprecation"/> </interceptor-stack>
Params拦截器是struts2默认的拦截器之一,其作用如下:
ModelDrivenInterceptor 的interceptor() 方法:
public String intercept(ActionInvocation invocation) throws Exception { //获取action对象 Object action = invocation.getAction(); //检查是否实现了ModelDriven接口 if ((action instanceof ModelDriven)) { ModelDriven modelDriven = (ModelDriven)action; //取值栈 ValueStack stack = invocation.getStack(); //获取Action中的成员变量对象 Object model = modelDriven.getModel(); if (model != null) { //压入值栈栈顶 stack.push(model); } if (refreshModelBeforeResult) { invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model)); } } //执行下一个拦截器操作 return invocation.invoke(); }
– 1. params 拦截器首先给 action 中的相关参数赋值,如 id– 2.prepare 拦截器执行 prepare 方法, prepare 方法中会根据参数,如 id, 去调用业务逻辑,设置 model 对象– 3. modelDriven 拦截器将 model 对象压入 value stack, 这里的 model 对象就是在 prepare 中创建的– 4. params 拦截器再将参数赋值给 model 对象– 5. action 的业务逻辑执行
<interceptor-stack name="paramsPrepareParamsStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="i18n"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="multiselect"/> <interceptor-ref name="params"> <param name="excludeParams">^action:.*,^method:.*</param> </interceptor-ref> <interceptor-ref name="servletConfig"/> <interceptor-ref name="prepare"/> <interceptor-ref name="chain"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"> <param name="excludeParams">^action:.*,^method:.*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack>