1. 需求
自定义拦截器实现,用户登录的访问控制。
2. 定义拦截器类
1 public class LoginInterceptor extends AbstractInterceptor 2 { 3 4 @Override 5 public String intercept(ActionInvocation ai) throws Exception 6 { 7 // TODO Auto-generated method stub 8 System.out.println("--------------intercept"); 9 String methodName = ai.getProxy().getMethod(); //获得被拦截的方法名字10 System.out.println("=============" + methodName);11 if (methodName.equals("userLogin"))12 {13 return ai.invoke();//放行:继续向下执行14 }15 //获得登陆成功之后,会话对象封装的数据16 Object obj = ai.getInvocationContext().getSession().get("user");17 if (obj == null)18 {19 return Action.LOGIN; //跳转到登陆页面20 } else21 {22 return ai.invoke();//放行:继续向下执行23 }24 }25 }
3. 配置拦截器
1 2 56 7 368 9 16 17 18[A2] 10 11 12 1513 14 19 21 22WEB-INF/jsp/login.jsp 2024 25 3026 /index.jsp 27/index.jsp 28WEB-INF/jsp/show.jsp 2931 35WEB-INF/jsp/show.jsp 32 3334