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

5 return " succee " ;
6 } < BR >
7
8 public String search(){ < BR >
9 System.out.println( " search

10 return " succee " ;
11 }
12 public String add(){
13 System.out.println( " add

14 return " succee " ;
15 }
16 } < BR >
17
struts.xml配置
2 < interceptor - ref name = " log " >< BR >
3 < param name = " name " > 日志拦截 </ param >< BR >
4 <!-- 设置需要拦截的方法,指定多个方法以逗号隔开 -->
5
6 < param name = " includeMethods " > execute,add </ param >< BR >
7 <!-- 设置不需要拦截的方法,execute在includeMethods中同时存在,execute会被拦截 -->< BR >
8 < param name = " excludeMethods " > search,execute </ param >
9 </ interceptor - ref >< BR >
10 < result name = " succee " >/ welcome.jsp </ result >
11 </ action >
12
打开浏览器访问 http://localhost:8080/struts2-interceptor/manage.action
会报执行execute方法,会执行拦截器
拦截器名称:日志拦截
action:com.ManageAction@1a0ae6d
execute....
当访问 http://localhost:8080/struts2-interceptor/manage!search.action
执行search方法,不会执行拦截器