struts2中默认拦截器栈中的拦截器…

本文介绍了Struts2框架中的默认拦截器栈,包括其工作原理和常见拦截器的作用,帮助理解Struts2的请求处理流程。
摘要由CSDN通过智能技术生成

1:  com.opensymphony.xwork2.interceptor.ExceptionMappingIntercep tor

        While you can configure exception mapping in your configuration file at any point, the configuration
        * will not have any effect if this interceptor is not in the interceptor stack for your actions.
        It is recommended that
        * you make this interceptor the first interceptor on the stack,
        ensuring that it has full access to catch any
        * exception, even those caused by other interceptors.
        这句话的意思是你可以配置异常在任何位置,只要这个拦截器没有加入生效的拦截器栈中,该拦截器就不会生效;
        官方建议,将异常拦截器配置在拦截器的第一个位置,因为只有这样才能更好的捕获异常,甚至是
        其他拦截器发生的异常;
        @Override
            public String intercept(ActionInvocation invocation) throws Exception {
            String result;

            try {
                  result = invocation.invoke();
            } catch (Exception e) {
                  if (isLogEnabled()) {
                  handleLogging(e);
                  }
                  List<ExceptionMappingConfig> exceptionMappings =
                  invocation.getProxy().getConfig().getExceptionMappings();
                  ExceptionMappingConfig mappingConfig =
                  this.findMappingFromException s(exceptionMappings, e);
                  if (mappingConfig != null && mappingConfig.getResult()!=null) {
                  Map parameterMap = mappingConfig.getParams();
                  // create a mutable HashMap since some interceptors will remove parameters, and parameterMap is immutable
                  invocation.getInvocationContext().setParameters(new HashMap<String, Object>(parameterMap));
                  result = mappingConfig.getResult();
                  publishException(invocation, new ExceptionHolder(e));
                  } else {
                  throw e;
                  }
            }

            return result;
            }
      这个拦截器的关键代码:
      这段代码的意思是,ActionInvocation调用栈中下一个的拦截器或者action对象(统称为元素);
      对这个调用进行try catch 捕获;
      如果调用这个方法不抛异常,直接将invoke方法的结果返回,回到ActionInvocation中继续执行栈中的下一个元素
      (可能是拦截器,也有可能是action类);
      如果调用invoke方法发生异常了对异常进行捕获;首先从配置文件中找到配置的所有异常处理配置项对应的那个map集合;
      用捕获的Exception为key去该map中找,看是否有对该异常的处理配置A,尤其是发生异常后的跳转页面B;
      如果has(A&&B),为false,即没有对应的配置,直接抛出该异常;交给服务器去管理;
      如果有该异常对应的处理配置,即异常的跳转页面;取出配置,
      将一些异常配置信息信息放入到ActionContext中的请求参数集合中去;
      同时将异常信息压入栈中,供调试获取显示输出用;
      异常处理完成;
      我们知道action的默认方法execute中抛出Exception父类;
      我们必须要知道,对应struts执行过程中可能出现的异常进行捕获,跳转到相应页面,尤其是
      业务逻辑异常,要给用户明确的提示;最后要配置异常和错误的父类,以捕获所有的异常;避免将异常直接抛给用户;
      为保险起见,在struts中配置了,在web.xml中也要进行配置;

2:  com.opensymphony.xwork2.interceptor.AliasInterceptor:
      这个拦截器的意思就是如果你在struts.xml文件中为action配置了参数;类似于这个
    <action name="register" class="com.itheima.web.struts.actions.RegisterAction" >
                          <inte
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值