Struts2中自定义拦截器
2012-02-27 11:20
274 查看
众所周知,Struts2的大部分功能都是通过拦截器实现的,就算是Struts2的启动也是使用Servlet的拦截器启动的,下
面我们来说一下自定义Struts2拦截器,其实只要是学过Servlet的拦截器,就应该明白大概原理,基本上是差不多的。
首先需要写一个类,实现Struts2的指定接口,com.opensymphony.xwork2.interceptor.Interceptor;
1 package com.bird.action;
2
3 import com.opensymphony.xwork2.ActionContext;
4 import com.opensymphony.xwork2.ActionInvocation;
5 import com.opensymphony.xwork2.interceptor.Interceptor;
6
7 public class PermissionInterceptor implements Interceptor {
8
9
10 private static final long serialVersionUID = 1L;
11
12 public void destroy() {
13 // TODO Auto-generated method stub
14
15 }
16
17 public void init() {
18 // TODO Auto-generated method stub
19
20 }
21
22 public String intercept(ActionInvocation action) throws Exception {
23 Object value = ActionContext.getContext().getSession().get("user");
24 if(value != null) return action.invoke();
25 ActionContext.getContext().put("message", "您没有权限登录");
26 return "message";
27 }
28
29 }
和Servlet拦截器类似,主要都是在intercept这个方法上了,这里判断session里面是否有值,如果有值就调用拦截器后
面的Action的方法,这样就完成了拦截器的任务,没有servlet里面的拦截器链。下面的工作就是注册拦截器,在
struts.xml这个配置文件里面这样配置。
30 <package name="niao" namespace="/t" extends="struts-default">
31 <interceptors>
32 <interceptor name="permission" class="com.bird.action.PermissionInterceptor"/>
33 <interceptor-stack name="permissionStack">
34 <interceptor-ref name="defaultStack"></interceptor-ref>
35 <interceptor-ref name="permission"></interceptor-ref>
36 </interceptor-stack>
37 </interceptors>
38
39 <global-results>
40 <result name="message">/WEB-INF/jsp/hello.jsp</result>
41 </global-results>
42
43 <action name="login_*" class="com.bird.action.Login" method="{1}">
44
45 <interceptor-ref name="permissionStack"/>
46 <result name="success">/WEB-INF/jsp/hello.jsp</result>
47
48 </action>
49 </package>
这里启用了拦截器栈,这个意思是因为一旦你定义了自己的拦截器就把struts默认的拦截器都给屏蔽掉了,这样的话
很多struts的功能就不能使用了,所以使用了拦截器栈,这样把struts默认的和你自定义的都放在了一个堆栈中,实现
了自定义和默认的拦截器的同时使用,默认堆栈的名称为defalutStack,这个名字可以在struts-default.xml文件里面找
到,不能写错了。然后定义了一个全局信息处理页面,最后在action定义里面直接把拦截器堆栈放到里面去就行了。
总体感觉还是挺简单的,至少和servlet差不多
面我们来说一下自定义Struts2拦截器,其实只要是学过Servlet的拦截器,就应该明白大概原理,基本上是差不多的。
首先需要写一个类,实现Struts2的指定接口,com.opensymphony.xwork2.interceptor.Interceptor;
1 package com.bird.action;
2
3 import com.opensymphony.xwork2.ActionContext;
4 import com.opensymphony.xwork2.ActionInvocation;
5 import com.opensymphony.xwork2.interceptor.Interceptor;
6
7 public class PermissionInterceptor implements Interceptor {
8
9
10 private static final long serialVersionUID = 1L;
11
12 public void destroy() {
13 // TODO Auto-generated method stub
14
15 }
16
17 public void init() {
18 // TODO Auto-generated method stub
19
20 }
21
22 public String intercept(ActionInvocation action) throws Exception {
23 Object value = ActionContext.getContext().getSession().get("user");
24 if(value != null) return action.invoke();
25 ActionContext.getContext().put("message", "您没有权限登录");
26 return "message";
27 }
28
29 }
和Servlet拦截器类似,主要都是在intercept这个方法上了,这里判断session里面是否有值,如果有值就调用拦截器后
面的Action的方法,这样就完成了拦截器的任务,没有servlet里面的拦截器链。下面的工作就是注册拦截器,在
struts.xml这个配置文件里面这样配置。
30 <package name="niao" namespace="/t" extends="struts-default">
31 <interceptors>
32 <interceptor name="permission" class="com.bird.action.PermissionInterceptor"/>
33 <interceptor-stack name="permissionStack">
34 <interceptor-ref name="defaultStack"></interceptor-ref>
35 <interceptor-ref name="permission"></interceptor-ref>
36 </interceptor-stack>
37 </interceptors>
38
39 <global-results>
40 <result name="message">/WEB-INF/jsp/hello.jsp</result>
41 </global-results>
42
43 <action name="login_*" class="com.bird.action.Login" method="{1}">
44
45 <interceptor-ref name="permissionStack"/>
46 <result name="success">/WEB-INF/jsp/hello.jsp</result>
47
48 </action>
49 </package>
这里启用了拦截器栈,这个意思是因为一旦你定义了自己的拦截器就把struts默认的拦截器都给屏蔽掉了,这样的话
很多struts的功能就不能使用了,所以使用了拦截器栈,这样把struts默认的和你自定义的都放在了一个堆栈中,实现
了自定义和默认的拦截器的同时使用,默认堆栈的名称为defalutStack,这个名字可以在struts-default.xml文件里面找
到,不能写错了。然后定义了一个全局信息处理页面,最后在action定义里面直接把拦截器堆栈放到里面去就行了。
总体感觉还是挺简单的,至少和servlet差不多
相关文章推荐
- Struts2自定义异常拦截器
- struts2自定义拦截器
- Struts2中自定义拦截器导致Action注入参数丢失
- Struts2自定义拦截器——完整实例代码
- Struts2自定义拦截器—Session超时的处理
- struts2声明式异常处理,友好的错误界面,自定义异常拦截器,
- .Java程序员从笨鸟到菜鸟之(四十七)细谈struts2(九)内置拦截器和自定义拦截器详解(附源码) 分类: 学习专区 框架Struts2 Java程序员从笨鸟到菜鸟 2012-05-08 12:
- struts2自定义一个拦截器
- Struts2的自定义拦截器
- Struts2自定义拦截器
- SSH-Struts2简单的自定义拦截器MethodFilterInterceptor
- Struts2第七篇【介绍拦截器、自定义拦截器、执行流程、应用】
- struts2自定义拦截器
- Struts2笔记——自定义拦截器
- Struts2自定义拦截器
- struts2 自定义拦截器
- struts2自定义拦截器
- Struts2的异常处理机制、自定义拦截器
- struts2 自定义拦截器、通配符
- Struts2 自定义拦截器(方法拦截器)