Sturts2拦截器的应用Interceptor

首先Action

@ParentPackage("mypakage")
public class testAct extends ActionSupport{
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        return super.execute();
    }
    @Action(value="test",results={@Result(location="index.jsp")}/*,interceptorRefs={@InterceptorRef(value = "myStack")}*/)
    public String test() throws Exception {
        System.out.println("我是Action");
        return SUCCESS;
    }

}

2种struts2.xml的配置方式


    <package name="mypakage" extends="struts-default">
        <interceptors>
            <interceptor name="myInterceptor" class="com.lz.interceptor.myInterceptor"></interceptor>
            <!-- 自定义的拦截器栈 -->
            <!--<interceptor-stack name="myStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="myInterceptor"></interceptor-ref> </interceptor-stack> --> 
            <!-- 系统默认的拦截器栈 -->
            <!-- <interceptor-stack name="defaultStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="myInterceptor"></interceptor-ref> </interceptor-stack> -->
        </interceptors>
    </package>

使用系统默认的方式就不用写注解了
使用自定义的需要注解@Action(value=”test”,results={@Result(location=”index.jsp”)}/,interceptorRefs={@InterceptorRef(value = “myStack”)}/)

拦截处理类:

public class myInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation arg0) throws Exception {
        System.out.println("我是拦截器------------>>");
        String ispass = arg0.invoke();
        return ispass;
    }
}

拦截器会比Action先执行

Index.jsp:

 <body>
    <a href="test.action">---------------</a>
 </body>

你可能感兴趣的:(struts2,Interceptor)