struts2拦截器10-11讲

来源:互联网 发布:美工感受 编辑:程序博客网 时间:2024/04/28 13:26

 第10讲:主要讲拦截器的原理:

用到反射机制。忘了去看视频:

视频中的例子,自己照写的:

 

第十一讲才说了下拦截器的配置,

       //拦截器配置

 

       //action配置

        //默认的拦截器。注意定义的位置,看dtd定义
        <default-interceptor-ref name="defaultStack"></default-interceptor-ref>

 

 

//拦截器一般实现com.opensymphony.xwork2.interceptor.Interceptor;

struts2提供默认的实现com.opensymphony.xwork2.interceptor.AbstractInterceptor;

只要继承这个类即可。重写它的intercept方法

 

//需要对某个方法拦截:继承com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

看struts2源代码:

实际上应该是调用的doIntercept(invocation);这个方法:

继承后重写这个方法即可。

源码中的属性:

 

//action配置

 

多个函数用逗号隔开,如果两个都配置了,像上面的test方法,还是会拦截的,就是includeMethods的一定会拦截。。

 

 

注册监听实现:com.opensymphony.xwork2.interceptor.PreResultListener;

    public void beforeResult(ActionInvocation invocation, String resultCode) {
    }

拦截器里注册invocation.addPreResultListener(new MyListener());

 

视频中讲的权限的验证:

就是在拦截器中,判断session中有没有用户,没有,则跳转到登录页面Action.LOGIN;

否则就继续执行invocation.invoke();

 

 

原创粉丝点击