对于Struts2的拦截器有两种方法实现,分别是拦截器类继承AbstractInterceptor类或者继承MethodFilterInterceptor类,
其中,继承AbstractInterceptor类的拦截器类会拦截Action中的所有方法,然而,拦截器类继承MethodFilterInterceptor类,就可以选择拦截的方法。
一、继承AbstractInterceptor类,实现拦截器
首先看一下拦截器类
UserInterceptor1.java
package com.struts.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class UserInterceptor1 extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
System.out.println("执行拦截器UserInterceptor1之前");
String retult = invocation.invoke();
System.out.println("执行拦截器UserInterceptor1之后");
return retult;
}
}
其中在项目中有三个类似的类,只不过打印的内容是“执行拦截器UserInterceptor2...”、“执行拦截器UserInterceptor3...”
之后,我们看一下操作用户的Action,该Action实现对用户的增、删、改、查
OperateUserAction.java
package com.struts.action;
import com.opensymphony.xwork2.ActionSupport;
public class OperateUserAction extends ActionSupport{
public String addUser(){
System.out.println("添加用户!");
return "success";
}
public String updateUser(){
System.out.println("修改用户!");
return "success";
}
public String findUser(){
System.out.println("查找用户!");
return "success";
}
public String deleteUser(){
System.out.println("删除用户!");
return "success";
}
}
最后是struts.xml的配置方法,OperateUserAction的配置使用通配符
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="user" extends="struts-default">
<interceptors>
<interceptor name="userInterceptor1" class="com.struts.interceptor.UserInterceptor1"></interceptor>
<interceptor name="userInterceptor2" class="com.struts.interceptor.UserInterceptor2"></interceptor>
<interceptor name="userInterceptor3" class="com.struts.interceptor.UserInterceptor3"></interceptor>
<interceptor-stack name="userInterceptor">
<interceptor-ref name="userInterceptor1"></interceptor-ref>
<interceptor-ref name="userInterceptor2"></interceptor-ref>
<interceptor-ref name="userInterceptor3"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="*UserAction" class="com.struts.action.OperateUserAction" method="{1}User">
<interceptor-ref name="userInterceptor"></interceptor-ref>
<result name="success">/success.jsp</result>
<result name="fail">/fail.jsp</result>
</action>
</package>
</struts>
运行结果:
执行拦截器UserInterceptor1之前
执行拦截器UserInterceptor2之前
执行拦截器UserInterceptor3之前
添加用户!
执行拦截器UserInterceptor3之后
执行拦截器UserInterceptor2之后
执行拦截器UserInterceptor1之后
执行拦截器UserInterceptor1之前
执行拦截器UserInterceptor2之前
执行拦截器UserInterceptor3之前
修改用户!
执行拦截器UserInterceptor3之后
执行拦截器UserInterceptor2之后
执行拦截器UserInterceptor1之后
观察一下上面的实例,可以发现,用户增删改查的操作方法集中在一个Action中,所以,如果对这个Action使用Interceptor的话,拦截器会拦截该Action的所有方法,
如果我们不想让删除操作呗拦截器拦截就行不通了,所以我们要使用下一种实现Interceptor的方法
二、继承MethodFilterInterceptor类,实现拦截器
首先,先看一下拦截器的实现
package com.struts.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class UserInterceptor1 extends MethodFilterInterceptor{
// 继承MethodFilterInterceptor使用的方法
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
System.out.println("执行拦截器UserInterceptor1之前");
String retult = invocation.invoke();
System.out.println("执行拦截器UserInterceptor1之后");
return retult;
}
// 继承AbstratInterceptor使用的方法
/*@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
System.out.println("执行拦截器UserInterceptor1之前");
String retult = invocation.invoke();
System.out.println("执行拦截器UserInterceptor1之后");
return retult;
}*/
}
再看一下修改后的struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="user" extends="struts-default">
<interceptors>
<interceptor name="userInterceptor1" class="com.struts.interceptor.UserInterceptor1"></interceptor>
<interceptor name="userInterceptor2" class="com.struts.interceptor.UserInterceptor2"></interceptor>
<interceptor name="userInterceptor3" class="com.struts.interceptor.UserInterceptor3"></interceptor>
<interceptor-stack name="userInterceptor">
<interceptor-ref name="userInterceptor1">
<param name="excludeMethods">deleteUser</param><!-- 排除deleteUser方法 -->
</interceptor-ref>
<interceptor-ref name="userInterceptor2"></interceptor-ref>
<interceptor-ref name="userInterceptor3">
<param name="includeMethods">addUser,updateUser,findUser</param><!-- 在addUser,updateUser,findUser方法中使用该拦截器 -->
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="*UserAction" class="com.struts.action.OperateUserAction" method="{1}User">
<interceptor-ref name="userInterceptor">
<param name="userInterceptor2.excludeMethods">deleteUser</param><!-- 排除deleteUser方法 -->
</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="fail">/fail.jsp</result>
</action>
</package>
</struts>
运行结果:
执行拦截器UserInterceptor1之前
执行拦截器UserInterceptor2之前
执行拦截器UserInterceptor3之前
添加用户!
执行拦截器UserInterceptor3之后
执行拦截器UserInterceptor2之后
执行拦截器UserInterceptor1之后
执行拦截器UserInterceptor1之前
执行拦截器UserInterceptor2之前
执行拦截器UserInterceptor3之前
修改用户!
执行拦截器UserInterceptor3之后
执行拦截器UserInterceptor2之后
执行拦截器UserInterceptor1之后
执行拦截器UserInterceptor1之前
执行拦截器UserInterceptor2之前
执行拦截器UserInterceptor3之前
查找用户!
执行拦截器UserInterceptor3之后
执行拦截器UserInterceptor2之后
执行拦截器UserInterceptor1之后
删除用户!
删除用户!
删除用户!
删除用户!
可以看出,删除用户操作执行了四次都没被拦截器拦截
源代码下载:点击打开链接