Struts2拦截器

struts.xml配置:
<interceptors>
   <interceptor name="myinterceptor" class="com.test.Interceptor.MyInterceptor"></interceptor>
   <interceptor name="myinterceptor2" class="com.test.Interceptor.Myinterceptor2"></interceptor>
   <interceptor-stack name="interceptorStack">
   <interceptor-ref name="myinterceptor"></interceptor-ref>
   <interceptor-ref name="defaultStack"></interceptor-ref>
   </interceptor-stack>
   </interceptors>
  
<action name="index" class="com.web.struts.LoginAction" method="test">
<result name="success">/output.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="myinterceptor2">
<param name="includeMethods">test,execute</param>
</interceptor-ref>
<interceptor-ref name="myinterceptor"></interceptor-ref>

</action>

拦截器栈包括拦截器和拦截器栈

Intercept接口拦截所有的方法
package com.test.Interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor implements Interceptor {

public void destroy() {
System.out.println("destory");
}

public void init() {
System.out.println("init");
}

public String intercept(ActionInvocation Invocation) throws Exception {
// TODO Auto-generated method stub
System.out.println("intercept");
String result = Invocation.invoke();
System.out.println("interceptEnd");
return result;
}

}

MethodFilterInterceptor
拦截特定的方法

package com.test.Interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class Myinterceptor2 extends MethodFilterInterceptor {

@Override
protected String doIntercept(ActionInvocation Invocation) throws Exception {
System.out.println("Myinterceptor2");
String result = Invocation.invoke();
System.out.println("Myinterceptor2End");
return result;
}

}

你可能感兴趣的:(struts2拦截器)