[Struts2] ModelDriven和Preparable拦截器

Struts2运行流程

  • ActionProxy是Action的一个代理类,也就是说Action的调用时通过ActionProxy实现的,其实就是调用了ActionProxy.execute()方法,而该方法又调用了ActionInvocation.invoke()方法
  • ActonInvocation就是Action的调用者,ActonInvocation在Action的执行过程中,负责Interceptor、Action和Result等一系列元素的调度
  • 拦截器的调用顺序是在struts-default.xml文件中的defaultStack中进行定义的

Params拦截器

Parameters拦截器将把表单字段映射到ValueStack栈的栈顶对象(此时栈顶对象即为Action)的各个属性中,如果某个字段在模型里没有匹配的属性,Param拦截器将尝试ValueStack栈的下一个对象。

把Action和Model隔开

Action作为Controller,应当与Model隔离开,这才是MVC设计模式。

如果Action类实现了ModelDriven接口,该拦截器将把ModelDriven接口的getModel()方法返回的对象置于栈顶。

Action实现ModelDriven接口后的运行流程:

  • 先会执行ModelDrivenInterceptor的intercept方法

    public String intercept(ActionInvocation invocation) throws Exception {
    // 获取Action对象,即EmpolyeeAction对象,此时该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()方法
      /*
        @Override
        public Employee getModel() {
            // TODO Auto-generated method stub
            employee = new Employee();
            return employee;
        }
      */
      Object model = modelDriven.getModel();
      if (model !=  null) {
        // 把getModel()方法的返回值压入到值栈的栈顶,实际压入的是EmployeeAction的employee成员变量
        stack.push(model);
      }
      if (refreshModelBeforeResult) {
        invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model));
      }
    }
    return invocation.invoke();
    }
  • 执行ParametersInterceptor的intercept方法:把请求参数的值赋给栈顶对象对应的属性,若栈顶对象没有对应的属性,则查询值栈中下一个对象对应的属性

  • 注意:getModel()方法不能提供以下实现,的确会返回一个Employee对象到值栈的栈顶,但当前Action的employee成员变量却是null:

    @Override
    public Employee getModel() {
    // TODO Auto-generated method stub
    return new Employee();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值