一.理解拦截器
1. 拦截器是在防问某个方法,字段之前或之后实施拦截,并且拦截器是可插拔的,拦截器是AOP的一种实现.
2. 拦截器栈(Interceptor Stack)。拦截器栈就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时, 拦截器链中的拦截器就会按其之前定义的顺序被调用。
二.实现原理
Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的 拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器
三.拦截器的配置
1. 普通的拦截器
- <package name="default" extends="struts-default"><BR>
- <interceptors><BR>
- <interceptor name="拦截器名1" class="拦截器实现类"/><BR>
- <interceptor name="拦截器名2" class="拦截器实现类"/><BR>
- </interceptors><BR>
- <action name="login" class="com.Logon"><BR>
- <interceptor-ref name="defaultStack"/><BR>
- <interceptor-ref name="拦截器名1"/><BR>
- <interceptor-ref name="拦截器名2"/><BR>
- <result name="input">logon.jsp</result><BR>
- <result name="success">/index.jsp</result><BR>
- </action><BR>
- </package>
<package name="default" extends="struts-default"> <interceptors> <interceptor name="拦截器名1" class="拦截器实现类"/> <interceptor name="拦截器名2" class="拦截器实现类"/> </interceptors> <action name="login" class="com.Logon"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="拦截器名1"/> <interceptor-ref name="拦截器名2"/> <result name="input">logon.jsp</result> <result name="success">/index.jsp</result> </action> </package>
2. 拦截器栈
- <package name="default" extends="struts-default"><BR>
- <interceptors><BR>
- <interceptor name="拦截器名1" class="拦截器实现类"/><BR>
- <interceptor name="拦截器名2" class="拦截器实现类"/><BR>
- <interceptor-stack name="myStack"><BR>
- <interceptor-ref name="拦截器名1"/><BR>
- <interceptor-ref name="拦截器名2"/><BR>
- </interceptor-stack><BR>
- </interceptors><BR>
- <action name="login" class="com.Logon"><BR>
- <interceptor-ref name="defaultStack"/><BR>
- <interceptor-ref name="myStack"/><BR>
- <result name="input">login.jsp</result><BR>
- <resultnameresultname="success" >/index.jsp</result><BR>
- </action><BR>
- </package><BR>
<package name="default" extends="struts-default"> <interceptors> <interceptor name="拦截器名1" class="拦截器实现类"/> <interceptor name="拦截器名2" class="拦截器实现类"/> <interceptor-stack name="myStack"> <interceptor-ref name="拦截器名1"/> <interceptor-ref name="拦截器名2"/> </interceptor-stack> </interceptors> <action name="login" class="com.Logon"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="myStack"/> <result name="input">login.jsp</result> <resultname="success" >/index.jsp</result> </action> </package>
需要注意的是,如果为Action指定了一个拦截器,则系统默认的拦截器栈将会失去作用。为了继续使用默认拦截器,所以上面配置文件中手动引入了默认拦截器
四.自定义拦截器
1.直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor。
2.或者继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor
3.通过<interceptor>元素来定义拦截器
4.通过<interceptor-ref>元素来使用拦截器
五.使用拦截器实现权限控制
1.功能
使用自定义拦截器来完成用户权限的控制,当执行操作时,判断用户是否己经登陆,如果没有登陆跳转到登陆页面
2.拦截器类
- package com.interceptor;
- <BR>
- <BR>import com.opensymphony.xwork2.Action;
- <BR>import com.opensymphony.xwork2.ActionContext;
- <BR>import com.opensymphony.xwork2.ActionInvocation;
- <BR>import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
- <BR>
- <BR>public class AuthorizationInterceptor extends AbstractInterceptor {
- <BR>
- <BR> private static final long serialVersionUID = 1L;
- <BR>
- <BR> @Override
- <BR> public String intercept(ActionInvocation actionInvocation) throws Exception {
- <BR>
- <BR> ActionContext actionContext = actionInvocation.getInvocationContext();
- <BR>
- <BR> Object user = actionContext.get("user");
- <BR>
- <BR> if(user != null){
- <BR> return actionInvocation.invoke();
- <BR> } else{
- <BR> actionInvocation.getInvocationContext().put("nav_title", "你还没有登陆,请先登陆");
- <BR> return Action.LOGIN;
- <BR> }
- <BR> }
- <BR>
- <BR>}
- <BR>
package com.interceptor;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthorizationInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
ActionContext actionContext = actionInvocation.getInvocationContext();
Object user = actionContext.get("user");
if(user != null){
return actionInvocation.invoke();
} else{
actionInvocation.getInvocationContext().put("nav_title", "你还没有登陆,请先登陆");
return Action.LOGIN;
}
}
}
2.配置
- <?xml version="1.0" encoding="UTF-8" ?><BR>
- <!DOCTYPE struts PUBLIC<BR>
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"<BR>
- "http://struts.apache.org/dtds/struts-2.0.dtd"><BR>
- <struts> <BR>
- <package name="user" extends="struts-default">
- <interceptors>
- <!-- 定义拦截器 -->
- <interceptor name="authority" class="com.interceptor.AuthorizationInterceptor"></interceptor>
- </interceptors><BR>
- <action name="user" class="com.UserAction"><BR>
- <!-- 使用拦截器 -->
- <interceptor-ref name="authority"/>
- <interceptor-ref name="defaultStack"/>
- <result name="succee">/logon/welcome.jsp</result><BR>
- <result name="login">/logon/logon.jsp</result>
- </action>
- </package>
- </struts><BR>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="user" extends="struts-default"> <interceptors> <!-- 定义拦截器 --> <interceptor name="authority" class="com.interceptor.AuthorizationInterceptor"></interceptor> </interceptors> <action name="user" class="com.UserAction"> <!-- 使用拦截器 --> <interceptor-ref name="authority"/> <interceptor-ref name="defaultStack"/> <result name="succee">/logon/welcome.jsp</result> <result name="login">/logon/logon.jsp</result> </action> </package> </struts>
访问http://localhost:8080/struts2-interceptor/user.action时,会判断用户是否登陆
六.方法拦截器
1.Struts2提供MethodFilterInterceptor类,该类是AbstractInerceptor的子类,可以实现对Action方法的拦截.
2. MethodFilterInterceptor中有两个方法
setExcludeMethods:排除需要过滤的方法
setIncludeMethods:设置需要过滤的方法
如果一个方法同时在excludeMethods和includeMethods中出现,则会被拦截
3.实现
拦截器
- package com.interceptor;
- <BR>
- <BR>import com.opensymphony.xwork2.ActionInvocation;
- <BR>import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
- <BR>
- <BR>public class LogInterceptor extends MethodFilterInterceptor {
- <BR>
- <BR> private static final long serialVersionUID = 1L;
- <BR>
- <BR> private String name;
- <BR>
- <BR> @Override
- <BR> protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
- <BR>
- <BR> System.out.println("拦截器名称:" + name);
- <BR> System.out.println("action:" + actionInvocation.getAction());
- <BR>
- <BR> return actionInvocation.invoke();
- <BR> }
- <BR>
- <BR> public String getName() {
- <BR> return name;
- <BR> }
- <BR>
- <BR> public void setName(String name) {
- <BR> this.name = name;
- <BR> }
- <BR>}
- <BR>
package com.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class LogInterceptor extends MethodFilterInterceptor {
private static final long serialVersionUID = 1L;
private String name;
@Override
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("拦截器名称:" + name);
System.out.println("action:" + actionInvocation.getAction());
return actionInvocation.invoke();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
action
- package com;<BR><BR>
- public class ManageAction {<BR><BR>
- public String execute(){
- System.out.println("execute....");<BR>
- return "succee";
- }<BR>
- public String search(){<BR>
- System.out.println("search....");
- return "succee";
- }
- public String add(){
- System.out.println("add....");
- return "succee";
- }
- }<BR>
package com;
public class ManageAction {
public String execute(){
System.out.println("execute....");
return "succee";
}
public String search(){
System.out.println("search....");
return "succee";
}
public String add(){
System.out.println("add....");
return "succee";
}
}
struts.xml配置
- <action name="manage" class="com.ManageAction"><BR>
- <interceptor-ref name="log"><BR>
- <param name="name">日志拦截</param><BR>
- <!-- 设置需要拦截的方法,指定多个方法以逗号隔开 -->
- <param name="includeMethods">execute,add</param><BR>
- <!-- 设置不需要拦截的方法,execute在includeMethods中同时存在,execute会被拦截 --><BR>
- <param name="excludeMethods">search,execute</param>
- </interceptor-ref><BR>
- <result name="succee">/welcome.jsp</result>
- </action>
<action name="manage" class="com.ManageAction"> <interceptor-ref name="log"> <param name="name">日志拦截</param> <!-- 设置需要拦截的方法,指定多个方法以逗号隔开 --> <param name="includeMethods">execute,add</param> <!-- 设置不需要拦截的方法,execute在includeMethods中同时存在,execute会被拦截 --> <param name="excludeMethods">search,execute</param> </interceptor-ref> <result name="succee">/welcome.jsp</result> </action>
打开浏览器访问 http://localhost:8080/struts2-interceptor/manage.action
会报执行execute方法,会执行拦截器
拦截器名称:日志拦截
action:com.ManageAction@1a0ae6d
execute....
当访问 http://localhost:8080/struts2-interceptor/manage!search.action
执行search方法,不会执行拦截器