您的位置:首页 > 编程语言 > Java开发

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

2015-12-24 10:28 435 查看
1: 
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor

   
 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.findMappingFromExceptions(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类);

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: