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

Struts拦截器

2017-03-01 00:00 369 查看

Struts拦截器

struts.xml配置文件中注册:

<interceptor
name="interceptorName"
class="interceptorClass"
/>
或者

<interceptor
name="interceptorName"
class="interceprotClass">
        <param
name="paramName">paramValue</param>
</interceptor>

拦截器栈:

<interceptors>
        <interceptor
name="interceptor1"
class="interceptorClass"
/>
        <interceptor
name="interceptor2"
class="interceprotClass"
/>
        <interceptor-stack
name="myStack">
            <interceptor-ref
name="interceptor1"
/>
            <interceptor-ref
name="interceptor2"
/>
        </interceptor-stack>
</interceptors>
配置默认拦截器(每个包下只能定义一个默认拦截器):

<default-interceptor-ref
name="myStack"
/>

 

只要自定义包继承了struts-default默认包,就会继承其内置的各个拦截器和其默认的拦截器栈。struts-dafault包的默认拦截器栈会作用于所有的这些Action。

 

使用拦截器时,需要在Action中进行配置,通过<interceptor-ref>标签类指定在Action中使用的拦截器。

<action
name="login"
class="com.chen.action.LoginAction">
        <result
name="input">/login.jsp</result>
        <interceptor-ref
name="interceptor1"
/>
        <interceptor-ref
name="interceptor2">
            <param
name="paramName">paramValue</param>
        </interceptor-ref>
</action>

Struts2的默认拦截器

alias拦截器

该拦截器的作用是给参数起一个别名,实现Action之间的传值。

在struts.xml文件中的<action>标签下,添加以下标签:

<param
name="aliases">#{'name1':'value1','name2':'value2'}</param>

在调用包含上面代码的Action时,Struts2框架会将Action莲中当前Action的前一个Action的name1和name2属性取出,赋给当前Action的value1和value2属性,这样就完成了Action之间的传值。

注释:从一个Action调到另一个Action,可以通过设置result元素的type属性来实现。有两种方法:第一种是将type设置为chain结果类型;第二种是将types属性设置为redirectAction类型。

prepare拦截器

该拦截器用来调用实现了Preparable接口的Action的prepare()方法。

如果一个Action实现了Preparable接口,则prepare拦截器会在执行execute()方法之前先执行prepare()方法。

         由于Action所在的包基本上都继承了struts-default包,因此也会继承其默认拦截器栈defaultStack,而在defaultStack中配置了prepare拦截器,所以prepare拦截器不需要在struts.xml文件中配置,就会被正确的调用。

timer拦截器

timer拦截器的功能是输出调用Action所需要的时间,它记录了execute方法和在timer后面定义的其他拦截器的intercept方法的执行时间之和,其时间单位为毫秒。

<action
name="login"
class="com.chen.action.LoginAction">
        <interceptor-ref
name="timer"
/>
        <result
name="input">/login.jsp</result>
</action>

 

自定义拦截器

         Struts2拦截器在访问某个Action 方法之前或之后实施拦截。

Struts2 拦截器是可插拔的, 拦截器是 AOP 的一种实现。

编写自定义拦截器

         自定义拦截器有两种定义方法,一种是要实现com.opensymphony.xwork2.interceptor.Interceptor接口;另一种是继承AbstractInterceptor类来定义拦截器。
(1)实现Interceptor接口定义拦截器:
publicclass PermissionInterceptor
implements Interceptor {
    privatefinalstaticlongserialVersionUID=1L;
    @Override
    publicvoid destroy() {
    }
    @Override
    publicvoid init() {
    }
    @Override
    public String intercept(ActionInvocation
invocation)
throws Exception {
        String
result=null;
        //提取session[username]
        Object
username=ActionContext.getContext().getSession().get("username");
        //验证是否登录(若登录会存在session)
        if(username!=null&&!username.equals("")){
             result=invocation.invoke();
        }else{
            result="login";
        }
        System.out.println(result);
        returnresult;
    }
}

(2)继承AbstractInterceptor类定义拦截器
publicclass
MyInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation
invocation)
throws Exception {
       
        //调用下一个拦截器
        String
result=invocation.invoke();
        returnresult;
    }
}

 

配置自定义拦截器

在struts.xml文件中的<package>标签下:

<interceptors>
        <interceptor
name="mylogin"
class="com.chen.interceptor.PermissionInterceptor"></interceptor>
</interceptors>

 

方法过滤拦截器

继承MethodFilterInterceptor类的子类能够有选择的拦截Action中的某个指定方法。

继承MethodFilterInterceptor类的子类必须实现doIntercept()方法,用来实现真正的拦截逻辑。

编写方法过滤拦截器:

publicclass
MyMethodFilterInterceptor extends MethodFilterInterceptor {
    @Override
    protected String doIntercept(ActionInvocation
invocation)
throws Exception {
        String
result=invocation.invoke();
        returnresult;
    }
}

在struts.xml中配置方法过滤拦截器

<interceptors>
        <interceptor
name="FilterInterceptor"
class="com.chen.interceptor.MyMethodFilterInterceptor"
/>
</interceptors>
    <action
name="filterAction"
class="com.chen.action.FilterAction">
        <result
name="success">/success.jsp</result>
        <interceptor-ref
name="FilterInterceptor">
            <!--
指定拦截方法 -->
            <param
name="includeMethods">execute,method1</param>
            <!--
指定不拦截方法 -->
            <param
name="excludeMethods">method2</param>
        </interceptor-ref>
    </action>

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