天天看点

Interceptor的简单使用

1.写一个类,testinterceptor 实现了interceptor的接口,在这里我们可以做任何事情,通常我做的是权限的拦截,可以根据你想要实现的功能来命名你的interceptor,比如权限的拦截器,可以取名checkprivilegeinterceptor。下面是拦截器类的代码

testinterceptor

package com.ssh.interceptor;

import com.opensymphony.xwork2.actioninvocation;

import com.opensymphony.xwork2.interceptor.interceptor;

public class testinterceptor implements interceptor {

    public void destroy() {

        system.out.println("---->> testinterceptor ---->> destory()");

    }

    public void init() {

        system.out.println("---->> testinterceptor ---->> init()");

    public string intercept(actioninvocation invocation) throws exception {

        system.out.println("---->> testinterceptor ---->> before()");

        invocation.invoke();

        system.out.println("---->> testinterceptor ---->> after()");

        return "test";

}

2.在你的struts.xml的配置文件里面配置刚刚的拦截器类就行了

struts.xml:

    <package name="default" namespace="/" extends="struts-default">

        <!-- 配置拦截器栈 默认的拦截器栈名为defaultstack -->

        <interceptors>

            <interceptor name="testinterceptor" class="com.ssh.interceptor.testinterceptor">

            </interceptor>

            <interceptor-stack name="defaultstack">

                <interceptor-ref name="testinterceptor"></interceptor-ref>

                  <interceptor-ref name="defaultstack"></interceptor-ref>

            </interceptor-stack>

        </interceptors>

   </package>

3.重新部署一下项目,查看控制台,如果没什么exception就没什么问题。