struts拦截器配置流程

1.新建拦截器实现MethodFilterInterceptor接口

public class PrivailegeInterceptor extends MethodFilterInterceptor {

    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {

       //获取并打印访问地址
         ActionProxy proxy = invocation.getProxy();
         String  actionName =  proxy.getActionName();
         String namespace = proxy.getNamespace();
         System.out.println("url为"+namespace+actionName);

        //1.获得session
        Map<String, Object> session = ActionContext.getContext().getSession();
        
        Object session1 = session.get("user");
        if(session1!=null){
        //2.有就放行
            invocation.invoke();
        }
        //3.没有就回到登录页面    这里注意需要配置全局结果集
        return "tolgoin";
    }
    
}

这里注意为什么要继承MethodFilterInterceptor接口 根据源码可以看到 其中定义了excludeMethods不拦截的()方法这样我们更易配置

  protected Set<String> excludeMethods = Collections.emptySet();
    protected Set<String> includeMethods = Collections.emptySet();

2.在struts配置文件中配置

 <package name="Action" namespace="/" extends="struts-default">
     
     <interceptors>
     <!-- 注册拦截器 -->
         <interceptor name="PrivailegeInteceptor" class="cn/txj/web/Interceptor/PrivailegeInterceptor.java"></interceptor>
         <!-- 注册拦截器栈(其中excludeMethods代表不执行的方法名称)-->
         <interceptor-stack name="systemInteceptor">
             <interceptor-ref name="PrivailegeInteceptor">
                 <param name="excludeMethods">regist,login</param>
             </interceptor-ref>
             <interceptor-ref name="defaultStack"></interceptor-ref>
         </interceptor-stack>
     </interceptors>
     <!-- 指定拦截器 -->
     <default-interceptor-ref name="systemInteceptor"></default-interceptor-ref>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值