ModeDriven 和Preparable拦截器 及其源码解析

在一般的类中,属性值需要有 getter 和setter 方法,但是在对应的action类中,需要用到的id属性值提供set方法, 不要提供get方法!!或者 两个类中的id属性名不要相同,不然值栈中赋值会错乱;

struts2 中的modelDriven拦截器负责把Action类以外的一个对象压入到值栈栈顶
prepare 拦截器负责准备为getModel()方法准备model


Action实现 ModelDriven 接口后的运行流程
    1. 先会执行 ModelDrivenInterceptor 的 intercept 方法.

    public String intercept(ActionInvocation invocation) throws Exception {
        //获取 Action 对象: EmployeeAction 对象, 此时该 Action 已经实现了 ModelDriven 接口
        //public class EmployeeAction implements RequestAware, ModelDriven<Employee>

   Object action = invocation.getAction();

  //判断 action 是否是 ModelDriven 的实例
   if (action instanceof ModelDriven) {
            //强制转换为 ModelDriven 类型
            ModelDriven modelDriven = (ModelDriven) action;

      //获取值栈
            ValueStack stack = invocation.getStack(); 

      //调用 ModelDriven 接口的 getModel() 方法、即调用 EmployeeAction 的 getModel() 方法

      /*
            public Employee getModel() {
                employee = new Employee();
                return employee;
            }
            */


            Object model = modelDriven.getModel();
            if (model !=  null) {
                //把 getModel() 方法的返回值压入到值栈的栈顶. 实际压入的是 EmployeeAction 的 employee 成员变量——这就是modelDriven主要做的事
                stack.push(model);
            }
            if (refreshModelBeforeResult) {
                invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model));
            }
        }
        return invocation.invoke();
    }

2). 执行 ParametersInterceptor 的 intercept 方法: 把请求参数的值赋给栈顶对象对应的属性. 若栈顶对象没有对应的属性, 则查询值栈中下一个对象对应的属性...

3). 注意: getModel 方法不能提供以下实现. 的确会返回一个 Employee 对象到值栈的栈顶,也会给这个对象赋值,可是再也不能对其进行操作,因为获取不了. 但当前 Action 的 employee 成员变量却是 null.

public Employee getModel() {

    return new Employee();
}   



使用paramsPrepareParamsStack 拦截器栈后的运行流程:
    1.paramsPrepareParamsStack 和 defaultStack 一样都是拦截器栈,而struts-default 包 默认使用的是defaultStack
    2.可以在Struts 配置文件中通过以下方式修改使用的默认的拦截器栈
        <default - interceptor -ref  name = "paramsPrepareParamsStack"></default - interceptor - ref>
    3.paramsPrepareParamsStack 拦截器在于 params - modelDriven - params ,所以可以先把请求参数赋给action类对应的属性,再根据赋给Action 的那个属性值决定压到值栈栈顶的对象,最后再为栈顶对象的属性赋值
                                      params - prepare -  modelDriven - params
    对于edit 操作而言
        先为Action类的id赋值
        根据id 从数据库中加载对应的对象,并放入到值栈的栈顶
        再为栈顶对象的id赋值(实际上id属性值已经存在)
        把栈顶对象回显在表单中

    4.关于回显:Struts2 表单标签会从 值栈中对应的属性值进行回显
    5.存在的问题:
        1.在执行删除的时候,id 不为空,但getModel方法却从数据库中加载了一个对象,不该加载!
        public Person getModel(){
            if ( id == 0 )
                person = new Person();
            else
                person = dao.get(id);
            return person;
        }
        2.查询全部信息的时候,也 new 了一个 person对象,浪费
    6. 解决方案:使用PrepareInterceptor 和 Prepareable 接口               
            prepare方法主要作用:为getModel()方法 准备model
    7). 关于 PrepareInterceptor

        [分析后得到的结论]

        若 Action 实现了 Preparable 接口, 则 Struts 将尝试执行 prepare[ActionMethodName] 方法,
        若 prepare[ActionMethodName] 不存在, 则将尝试执行 prepareDo[ActionMethodName] 方法.
        若都不存在, 就都不执行.

        若 PrepareInterceptor  的 alwaysInvokePrepare 属性为 false,
        则 Struts2 将不会调用实现了 Preparable 接口的  Action 的 prepare() 方法

        [能解决 5  的问题的方案]

        可以为每一个 ActionMethod 准备 prepare[ActionMethdName] 方法, 而抛弃掉原来的 prepare() 方法。delete不用
        将 PrepareInterceptor  的 alwaysInvokePrepare 属性置为 false, 以避免 Struts2 框架再调用 prepare() 方法.——PrepareInterceptor 拦截器会根据 alwaysInvokePrepare 属性决定是否执行prepare()方法

        如何在配置文件中为拦截器栈的属性赋值: 参看 /struts-2.3.15.3/docs/WW/docs/interceptors.html

        <interceptors>
            <interceptor-stack name="parentStack">
            <interceptor-ref name="defaultStack">
                <param name="params.excludeParams">token</param>
            </interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="parentStack"/>

        ----------------------------------源代码解析---------------------------------

        public String doIntercept(ActionInvocation invocation) throws Exception {
            //获取 Action 实例
            Object action = invocation.getAction();

            //判断 Action 是否实现了 Preparable 接口
            if (action instanceof Preparable) {
            try {
                String[] prefixes;
                //根据当前拦截器的 firstCallPrepareDo(默认为 false) 属性确定 prefixes
                if (firstCallPrepareDo) {
                prefixes = new String[] {ALT_PREPARE_PREFIX, PREPARE_PREFIX};
                } else {
                prefixes = new String[] {PREPARE_PREFIX, ALT_PREPARE_PREFIX};
                }
                //若为 false, 则 prefixes: prepare, prepareDo
                //调用前缀方法.
                PrefixMethodInvocationUtil.invokePrefixMethod(invocation, prefixes);
            }
            catch (InvocationTargetException e) {

                Throwable cause = e.getCause();
                if (cause instanceof Exception) {
                throw (Exception) cause;
                } else if(cause instanceof Error) {
                throw (Error) cause;
                } else {
                throw e;
                }
            }

                //根据当前拦截器的 alwaysInvokePrepare(默认是 true) 决定是否调用 Action 的 prepare 方法
            if (alwaysInvokePrepare) {
                ((Preparable) action).prepare();
            }
            }

            return invocation.invoke();
        }

        PrefixMethodInvocationUtil.invokePrefixMethod(invocation, prefixes) 方法:

        public static void invokePrefixMethod(ActionInvocation actionInvocation, String[] prefixes) throws InvocationTargetException, IllegalAccessException {
            //获取 Action 实例
            Object action = actionInvocation.getAction();
            //获取要调用的 Action 方法的名字(update)
            String methodName = actionInvocation.getProxy().getMethod();

            if (methodName == null) {
                // if null returns (possible according to the docs), use the default execute
            methodName = DEFAULT_INVOCATION_METHODNAME;
            }

            //获取前缀方法
            Method method = getPrefixedMethod(prefixes, methodName, action);

            //若方法不为 null, 则通过反射调用前缀方法
            if (method != null) {
                method.invoke(action, new Object[0]);
            }
        }

        PrefixMethodInvocationUtil.getPrefixedMethod 方法:

        public static Method getPrefixedMethod(String[] prefixes, String methodName, Object action) {
            assert(prefixes != null);
            //把方法的首字母变为大写
            String capitalizedMethodName = capitalizeMethodName(methodName);

            //遍历前缀数组
            for (String prefixe : prefixes) {
            //通过拼接的方式, 得到前缀方法名: 第一次 prepareUpdate, 第二次 prepareDoUpdate
            String prefixedMethodName = prefixe + capitalizedMethodName;
            try {
                //利用反射获从 action 中获取对应的方法, 若有直接返回. 并结束循环.
                return action.getClass().getMethod(prefixedMethodName, EMPTY_CLASS_ARRAY);
            }
            catch (NoSuchMethodException e) {
                // hmm -- OK, try next prefix
                if (LOG.isDebugEnabled()) {
                LOG.debug("cannot find method [#0] in action [#1]", prefixedMethodName, action.toString());
                }
            }
            }
            return null;
        }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值