struts2 18拦截器详解(六)
PrepareInterceptor
该拦截器处于defaultStack第五的位置,该拦截器的功能主要是在Action的execute(假设配置文件中没有指定)方法之前执行一些业务逻辑,如果你刚好有这样的需求,该拦截器是你很好的选择,要使该拦截有效,Action要实现Preparable接口,与前面几个拦截器不同的是该拦截器继承自MethodFilterInterceptor而不是直接继承自AbstractInterceptor,MethodFilterInterceptor可以对拦截器的方法进行过滤,MethodFilterInterceptor拦截器将在ParametersInterceptor拦截器时一起讲解,暂时就可以理解为与直接继承自AbstractInterceptor无异,该拦截器中的doIntercept方法理解为以前的intercept方法,是处理功能逻辑的地方。下面是doIntercept方法的源码:
public static Method getPrefixedMethod(String[] prefixes, String methodName, Object action) {assert(prefixes != null);//把方法名称的第一个字母大写并返回,这个可以自己参看一下源码String capitalizedMethodName = capitalizeMethodName(methodName); for (String prefixe : prefixes) {//迭代前缀数组 //这里就是将要执行的方法名首字母大后前面加上相应的前缀(prepare或prepareDo) String prefixedMethodName = prefixe + capitalizedMethodName; try {//返回该方法,prepare或prepareDo中可有一个生效 return action.getClass().getMethod(prefixedMethodName, EMPTY_CLASS_ARRAY); } catch (NoSuchMethodException e) { // hmm -- OK, try next prefix if (LOG.isDebugEnabled()) { LOG.debug("cannot find method [" + prefixedMethodName + "] in action [" + action + "]"); } } }return null;}