Struts2拦截器
1、定义自己的拦截器
1.1拦截器类配置
继承MethodFilterInterceptor来定义自己的拦截器
package inceceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("前置拦截开始");
String result = actionInvocation.invoke();
System.out.println("后置拦截开始");
return result;
}
}
1.2 Action类配置
这个没啥说的就是配置action
package inceceptor;
import com.opensymphony.xwork2.ActionSupport;
public class InterceptorAction extends ActionSupport {
public String add() {
return SUCCESS;
}
}
1.3配置文件配置
<!--定义自己配置的拦截器-->
<interceptors>
<interceptor name="myinterceptor" class="inceceptor.MyInterceptor"/>
</interceptors>
<!--引用自己定义的拦截器-->
<interceptor-ref name="myinterceptor"/>
<package name="interceptor" extends="struts-default" namespace="/interceptor">
<!--定义自己配置的拦截器-->
<interceptors>
<interceptor name="myinterceptor" class="inceceptor.MyInterceptor"/>
</interceptors>
<action name="add" class="inceceptor.InterceptorAction" method="add">
<!--引用自己定义的拦截器-->
<interceptor-ref name="myinterceptor"/>
<result name="success">/success.jsp</result>
</action>
</package>
2、拦截器和拦截器栈(配置自己的拦截器栈)
1.1拦截器类配置
package inceceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("前置拦截开始");
String result = actionInvocation.invoke();
System.out.println("后置拦截开始");
return result;
}
}
1.2 Action类配置
不管是delete 还是add都可被拦截到
package inceceptor;
import com.opensymphony.xwork2.ActionSupport;
public class InterceptorAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete() {
return "delete";
}
}
1.3配置文件配置
<interceptors>
<interceptor name="myinterceptor" class="inceceptor.MyInterceptor"/>
<!--定义自己的拦截器栈-->
<interceptor-stack name="myinterceptorstack">
<!--引入struts2默认拦截器-->
<interceptor-ref name="defaultStack"/>
<!--引入自己定义的拦截器-->
<interceptor-ref name="myinterceptor"/>
</interceptor-stack>
</interceptors>
<!--设置全局默认拦截器-->
<default-interceptor-ref name="myinterceptorstack"/>
<!--定义全局拦截器包-->
<package name="mydefaultInterceptor" extends="struts-default" abstract="true">
<interceptors>
<interceptor name="myinterceptor" class="inceceptor.MyInterceptor"/>
<!--定义自己的拦截器栈-->
<interceptor-stack name="myinterceptorstack">
<!--引入struts2默认拦截器-->
<interceptor-ref name="defaultStack"/>
<!--引入自己定义的拦截器-->
<interceptor-ref name="myinterceptor"/>
</interceptor-stack>
</interceptors>
<!--设置全局默认拦截器-->
<default-interceptor-ref name="myinterceptorstack"/>
</package>
<!--继承mydefaultInterceptor 而不是default satack 这样才能使用自定义拦截器栈-->
<package name="interceptor" extends="mydefaultInterceptor" namespace="/interceptor">
<action name="add" class="inceceptor.InterceptorAction" method="add">
<result name="success">/success.jsp</result>
</action>
<action name="delete" class="inceceptor.InterceptorAction" method="delete">
<result name="delete">/success.jsp</result>
</action>
</package>
3、动作类的排除(动作类排除不需要的拦截器)
1.1拦截器类配置
package inceceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("前置拦截开始");
String result = actionInvocation.invoke();
System.out.println("后置拦截开始");
return result;
}
}
1.2 Action类配置
配置文件配置后add会被拦截到 delete 不会被拦截到 被排除在外
package inceceptor;
import com.opensymphony.xwork2.ActionSupport;
public class InterceptorAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete() {
return "delete";
}
}
1.3配置文件配置
<!--引用全局拦截器-->
<interceptor-ref name="myinterceptorstack">
<!--排除不需要的拦截器-->
<param name="myinterceptor.excludeMethods">delete</param>
</interceptor-ref>
<!--定义全局拦截器包-->
<package name="mydefaultInterceptor" extends="struts-default" abstract="true">
<interceptors>
<interceptor name="myinterceptor" class="inceceptor.MyInterceptor"/>
<!--定义自己的拦截器栈-->
<interceptor-stack name="myinterceptorstack">
<!--引入struts2默认拦截器-->
<interceptor-ref name="defaultStack"/>
<!--引入自己定义的拦截器-->
<interceptor-ref name="myinterceptor"/>
</interceptor-stack>
</interceptors>
<!--设置全局默认拦截器-->
<default-interceptor-ref name="myinterceptorstack"/>
</package>
<!--继承mydefaultInterceptor 而不是default satack 这样才能使用自定义拦截器栈-->
<package name="interceptor" extends="mydefaultInterceptor" namespace="/interceptor">
<action name="add" class="inceceptor.InterceptorAction" method="add">
<result name="success">/success.jsp</result>
</action>
<action name="delete" class="inceceptor.InterceptorAction" method="delete">
<!--引用全局拦截器-->
<interceptor-ref name="myinterceptorstack">
<!--排除不需要的拦截器-->
<param name="myinterceptor.excludeMethods">delete</param>
</interceptor-ref>
<result name="delete">/success.jsp</result>
</action>
</package>