zoukankan      html  css  js  c++  java
  • 自定义拦截器

    本人初学者,学习记录,供各人参考,若有错误,望指正,不胜感激!!

    经典实例:自定义权限拦截器

    判断用户是否登陆成功,看session中是否有用户信息即可,存在用户信息则表示已经登录成功,不存在用户信息则表示未登录

    1)a:定义需要访问拦截器的方法:

    1 package com.bjyinfu.struts.actions;
    2 
    3 public class SystemAction {
    4 
    5     public String execute(){
    6         return "success";
    7     }
    8 }

    b:定义权限拦截器:

     1 package com.bjyinfu.struts.interceptors;
     2 
     3 import com.opensymphony.xwork2.ActionContext;
     4 import com.opensymphony.xwork2.ActionInvocation;
     5 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
     6 
     7 //此拦截器的类名叫什么都行,所以要进行注册,在struts.xml中进行注册
     8 public class PremissionInterceptor extends AbstractInterceptor {
     9 
    10     @Override
    11     public String intercept(ActionInvocation invocation) throws Exception {
    12         //获取session中对应的key->user对应的value值
    13         String user = (String) ActionContext.getContext().getSession().get("user");
    14         if(user!=null){
    15             //当user中有信息则证明用户已经登陆成功,接下来会调用对应的Action方法
    16             //调用Action方法
    17             return invocation.invoke();
    18         }
    19         //user为null时表示session中没有用户信息,则用户未登录,此时会转跳到该Action对应的fail视图中
    20         return "fail";
    21     }
    22 
    23 }

    c:将权限拦截器同时注册到struts.xml文件中

    自定义拦截器注册方法一:拦截器注册方式

    自定义拦截器注册方法二:拦截器栈注册方式

    自定义拦截器注册方法三:将自定义拦截器与struts2的默认拦截器封装成一个统一的默认拦截器

    以上三种方法均可实现自定义拦截器的注册;

     2)方法过滤拦截器,用于指定Action类中的某个方法执行该拦截器,或不执行该拦截器:

    a:定义过滤拦截器:

     1 package com.bjyinfu.struts.interceptors;
     2 
     3 import com.opensymphony.xwork2.ActionContext;
     4 import com.opensymphony.xwork2.ActionInvocation;
     5 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
     6 import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
     7 
     8 //自定义过滤拦截器,继承了MethodFilterInterceptor类,并重写了doInterceptor方法
     9 public class FilterInterceptor extends MethodFilterInterceptor {
    10 
    11     @Override
    12     protected String doIntercept(ActionInvocation invocation) throws Exception {
    13         System.out.println("执行过滤器拦截器");
    14         return invocation.invoke();
    15     }
    16 
    17 
    18 }

    b:定义要执行过滤拦截器的方法:

     1 package com.bjyinfu.struts.actions;
     2 
     3 public class FilterInterceptorAction {
     4 
     5     public String doFirst(){
     6         System.out.println("执行doFirst");
     7         return "success";
     8     }
     9     public String doSecond(){
    10         System.out.println("执行doSecond");
    11         return "success";
    12     }
    13     public String doThird(){
    14         System.out.println("执行doThird");
    15         return "success";
    16     }
    17 }

    c:将过滤拦截器和方法注册到struts.xml中:

    拦截器的执行顺序:顺序与自定义拦截器栈中的拦截器注册顺序是一致的;

  • 相关阅读:
    23.2 编写笨的程序
    python面向对象
    面向对象封装
    python新式类删改查
    os模块sys模块 进度条实例
    选课系统练习程序
    python类相关
    xml模块
    json&pickle模块shelve模块
    Python写的ATM程序
  • 原文地址:https://www.cnblogs.com/lubolin/p/7444633.html
Copyright © 2011-2022 走看看