传智播客学习日记Day24

今天是学习Struts的第二天,这天要学习的主要有:拦截器(如何配置拦截器,拦截器的原理,编写自己的拦截器)。
老师首先对拦截器进行了基础讲解,然后教我们编写一个拦截器,可以拦截未登入的用户,直接访问特定的action.

拦截器
Struts2为一个Action自动注入的各种功能都是通过各种拦截器实施上去的。

自己定义一个拦截器,需要继承AbstractInterceptor,或实现Interceptor
    public class LogIntegerceptor extends AbstractInterceptor {
    
    @Override
    public void init() {
        System.out.println("LogIntegerceptor init");
    }

    @Override
    public void destroy() {
        System.out.println("LogIntegerceptor destroy");
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println(" LogIntegerceptor  intercept");
        return invocation.invoke();
    }
}

配置拦截器
    在package中定义一个拦截器
        <interceptors>
            <interceptor name="logInterceptor" class="cd.itcast.struts2day24.log.LogIntegerceptor"></interceptor>
        </interceptors>
        
 使用拦截器,使用interceptor-ref把指定名称的拦截器配置到action上.如果在某个action使用interceptor-ref配置了拦截器以后,默认的拦截器将不起作用.
        1.
        <interceptor-ref name="defaultStack"></interceptor-ref>
        <interceptor-ref name="logInterceptor"></interceptor-ref>
        

        2.
          配置一个拦截器栈
            <interceptor-stack name="myStack">
                <interceptor-ref name="logInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>
        使用一个拦截器栈进行拦截
            <interceptor-ref name="myStack"></interceptor-ref>
        
        3.
        <!--  默认的拦截器,如果actio中没有另外指定拦截器,则这里的拦截器是通用的 -->
        <default-interceptor-ref name="myStack"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值