【Struts2框架】Struts2中拦截器

1.拦截器

1.1什么是拦截器:在访问某个Action或者Action中的业务方法的时候,请求会经过一系列拦截器;当Result返回视图的时候也会经过一系列拦截器,真正有了这些拦截器的协助,才能使Struts2框架具备有很多自动化的功能(封装数据,类型转换,存储数据到作用域....),Struts2的拦截器是可拔插式的。

2.拦截器的分类:
 
内置拦截器:struts2框架默认自带的拦截器
在struts2-core-2.3.33.jar中有一个struts-default.xml配置文件,配置了struts2框架的基本设置,包含:常量的定义/自动实例化对象/10中result标签的type类型的定义/内置拦截器的定义
理解:struts.xml中package标签中 extends属性的作用,继承struts-default.xml中的
<package name="struts-default" abstract="true">,那么这个默认包中的result标签的type类型和内置拦截器都可以自动使用
 
自定义拦截器:根据需要自己开发( 注意:配置了自定义拦截器,那么内置拦截器会失效 )
开发步骤:(可以借鉴过滤器Filter)
 
a.自定义一个实现Interceptor接口(或者继承自AbstractInterceptor)的类
 
public class MyInterceptor implements Interceptor{
    public MyInterceptor() {
        System.out.println("MyInterceptor拦截器被实例化");
    }
    
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        System.out.println("MyInterceptor拦截器调用destroy()方法");
    }
 
    @Override
    public void init() {
        // TODO Auto-generated method stub
        System.out.println("MyInterceptor拦截器调用init()方法");
    }
 
    @Override
    public String intercept(ActionInvocation arg0) throws Exception {
        System.out.println("MyInterceptor拦截器调用intercept()方法");
        return invocation.invoke();
    }
 
}
 

b.在strutx.xml中注册上一步中定义的拦截器

<package name="users"  namespace="/"  extends="struts-default">
        <!-- 定义拦截器 -->
        <interceptors>
            <interceptor name=" myInterceptor " class="com.svse.interceptor.MyInterceptor"></interceptor>
        </interceptors>
 
......

c.

3.拦截器的生命周期
 
服务器启动的时候拦截器会被实例化,并且调用init()方法,当向对应Action发送请求的时候,或者离开Action的时候会调用intercept()方法;当服务器管理的时候调用destory()方法,然后销毁拦截器的实例
struts2拦截器是在访问某个Action或Action的某个方法,字段之前或者之后实施拦截,并且struts2拦截器是可拔插式,拦截器是Aop的一种实现
 
4. 拦截器栈(Interceptor Stack)
Struts2拦截器栈就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,Struts2拦截器链中的拦截器就会按其之前定义的顺序被调用 拦截器栈:多个拦截器组成的集合,使用这个拦截器的集合就相当于同时使用了多个拦截器
<interceptors>
            <interceptor name="myInterceptor" class="com.svse.interceptor.MyInterceptor"></interceptor>
            <interceptor name="timeInterceptor" class="com.svse.interceptor.TimeInterceptor"></interceptor>
            <interceptor name="sessionInterceptor" class="com.svse.interceptor.SessionInterceptor"></interceptor>
            <interceptor-stack name="interceptor-stack01">
                <interceptor-ref name="defaultStack"></interceptor-ref>
                <interceptor-ref name="myInterceptor"></interceptor-ref>
            </interceptor-stack>
            
            <interceptor-stack name="interceptor-stack02">
                <interceptor-ref name="defaultStack"></interceptor-ref>
                <interceptor-ref name="myInterceptor"></interceptor-ref>
                <interceptor-ref name="timeInterceptor"></interceptor-ref>
            </interceptor-stack>
            
            <interceptor-stack name="interceptor-stack03">
                <interceptor-ref name="defaultStack"></interceptor-ref>
                <interceptor-ref name="sessionInterceptor"></interceptor-ref>
            </interceptor-stack>
        </interceptors>
 
....
<action name="users*Action" class="com.svse.action.UsersAction" method="{1}">
            <result name="loginSuccess">/WEB-vINF/jsp/main.jsp</result>  
            <!-- 调用拦截器栈 -->
            <interceptor-ref name="interceptor-stack01"></interceptor-ref>        
        </action>       
.....
 
在struts.xml配置文件中,拦截器在不同的package
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值