struts2 18拦截器详解(六)
2013-10-31 11:40
381 查看
PrepareInterceptor
该拦截器处于defaultStack第五的位置,该拦截器的功能主要是在Action的execute(假设配置文件中没有指定)方法之前执行一些业务逻辑,如果你刚好有这样的需求,该拦截器是你很好的选择,要使该拦截有效,Action要实现Preparable接口,与前面几个拦截器不同的是该拦截器继承自MethodFilterInterceptor而不是直接继承自AbstractInterceptor,MethodFilterInterceptor可以对拦截器的方法进行过滤,MethodFilterInterceptor拦截器将在AnnotationValidationInterceptor拦截器时一起讲解,暂时就可以理解为与直接继承自AbstractInterceptor无异,该拦截器中的doIntercept方法理解为以前的intercept方法,是处理功能逻辑的地方。下面是doIntercept方法的源码:
该方法中逻辑很简单,主要逻辑在PrefixMethodInvocationUtil.invokePrefixMethod方法中,执行时把ActionInvocation对象与两个前缀字符串放在一个数组中传给了该方法,两个前缀分别是"prepare","prepareDo",这两个值是在该拦截器中写死了的。现在去看一下这个方法的源码:
下面我们去看一下getPrefixedMethod(prefixes, methodName, action);,看它是如何获取前缀方法的:
到这里前缀方法的获取与调用过程已经讲解完成,然后就是判断是否总是要执行prepare方法,如果alwaysInvokePrepare的值为true则会执行Action的prepare方法,prepare方法就是在Preparable接口中声明的方法。
最后执行invocation.invoke();调用下一个拦截器......
该拦截器处于defaultStack第五的位置,该拦截器的功能主要是在Action的execute(假设配置文件中没有指定)方法之前执行一些业务逻辑,如果你刚好有这样的需求,该拦截器是你很好的选择,要使该拦截有效,Action要实现Preparable接口,与前面几个拦截器不同的是该拦截器继承自MethodFilterInterceptor而不是直接继承自AbstractInterceptor,MethodFilterInterceptor可以对拦截器的方法进行过滤,MethodFilterInterceptor拦截器将在AnnotationValidationInterceptor拦截器时一起讲解,暂时就可以理解为与直接继承自AbstractInterceptor无异,该拦截器中的doIntercept方法理解为以前的intercept方法,是处理功能逻辑的地方。下面是doIntercept方法的源码:
@Override public String doIntercept(ActionInvocation invocation) throws Exception { Object action = invocation.getAction(); if (action instanceof Preparable) {//判断当前Action是否实现了Preparable接口 try {//如果实现了调用前缀方法 PrefixMethodInvocationUtil.invokePrefixMethod(invocation, new String[]{PREPARE_PREFIX, ALT_PREPARE_PREFIX}); } catch (InvocationTargetException e) { LOG.warn("an exception occured while trying to execute prefixed method", e); } catch (IllegalAccessException e) { LOG.warn("an exception occured while trying to execute prefixed method", e); } catch (Exception e) { LOG.warn("an exception occured while trying to execute prefixed method", e); } //是否总是要执行prepare方法,alwaysInvokePrepare的默认值为true if (alwaysInvokePrepare) { ((Preparable) action).prepare(); } } return invocation.invoke();//调用下一个拦截器 }
该方法中逻辑很简单,主要逻辑在PrefixMethodInvocationUtil.invokePrefixMethod方法中,执行时把ActionInvocation对象与两个前缀字符串放在一个数组中传给了该方法,两个前缀分别是"prepare","prepareDo",这两个值是在该拦截器中写死了的。现在去看一下这个方法的源码:
public static void invokePrefixMethod(ActionInvocation actionInvocation, String[] prefixes) throws InvocationTargetException, IllegalAccessException { Object action = actionInvocation.getAction();//获取当前执行的Action //获取Action要执行的方法名称 String methodName = actionInvocation.getProxy().getMethod(); //如果方法名为null则说明在struts2的配置文件中没有配置Action要执行的方法名称 if (methodName == null) { // 如果为null则表示执行execute方法,DEFAULT_INVOCATION_METHODNAME值就是"execute" methodName = DEFAULT_INVOCATION_METHODNAME; } //获取前缀方法 Method method = getPrefixedMethod(prefixes, methodName, action); if (method != null) {//如果不为null则调用前缀方法 method.invoke(action, new Object[0]); } }
下面我们去看一下getPrefixedMethod(prefixes, methodName, action);,看它是如何获取前缀方法的:
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; }
到这里前缀方法的获取与调用过程已经讲解完成,然后就是判断是否总是要执行prepare方法,如果alwaysInvokePrepare的值为true则会执行Action的prepare方法,prepare方法就是在Preparable接口中声明的方法。
最后执行invocation.invoke();调用下一个拦截器......
相关文章推荐
- Js与Java的基本数据类型
- uploadify 2.1.0 java spring mvc 2003版excel 附件上传
- myEclipse使用技巧:查看某方法/属性被调用的情况
- struts配置文件l中使用include 标签 引入另外一个在jar中的struts文件
- Java 自定义MVC示例
- JAVA日期工具类
- m着色(java)实现
- java生成 UML 类图
- myEclipse8.5生成类图UML
- struts2拦截器demo
- maven的eclipse插件在增加依赖(Add Dependency)时无法搜索远程仓库
- maven的eclipse插件在增加依赖(Add Dependency)时无法搜索远程仓库
- java 对map或hashmap排序的两种方法
- Java代码验证18位身份证号码是不是真的
- input 为disabled 引起的java.lang.NullPointerException
- java如何实现系统监控、系统信息收集、sigar开源API的学习
- Top 8 Diagrams for Understanding Java
- SpringAOP 详解介绍
- phonegap开发之------在eclipse4.2中安装aptana插件
- Java中Class.forName()的作用(转载)