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

    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         } else
    21         {
    22             return ai.invoke();//放行:继续向下执行
    23         }
    24     }
    25 }

    3. 配置拦截器

     1 <?xml version="1.0" encoding="UTF-8"?>
     2  <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 <struts>
     6     <package name="user" namespace="/" extends="struts-default">
     7         <interceptors>
     8             <!-- 声明自定义拦截器 -->
     9             <interceptor name="loginInterceptor" class="com.bjsxt.util.LoginInterceptor"></interceptor>[A2] 
    10             <!-- 自定义拦截器栈 -->
    11             <interceptor-stack name="mystack">
    12                 <interceptor-ref name="defaultStack"></interceptor-ref>
    13                 <interceptor-ref name="loginInterceptor"></interceptor-ref>
    14             </interceptor-stack>
    15         </interceptors>
    16     
    17         <!-- 全局的跳转结果 -->
    18         <global-results>
    19             <result name="login">WEB-INF/jsp/login.jsp</result>
    20         </global-results>
    21     
    22         <action name="userAction_*" class="com.bjsxt.action.UserAction"
    23                 method="{1}">
    24             <!-- 引用自定义拦截器栈 -->
    25             <interceptor-ref name="mystack"></interceptor-ref>
    26             <result name="input">/index.jsp</result>
    27             <result>/index.jsp</result>
    28             <result name="show">WEB-INF/jsp/show.jsp</result>
    29         </action>
    30         <action name="defAction">
    31             <result>WEB-INF/jsp/show.jsp</result>
    32             <!-- 引用自定义拦截器栈 -->
    33             <interceptor-ref name="mystack"></interceptor-ref>
    34         </action>
    35     </package>
    36 </struts>
  • 相关阅读:
    RestEasy+用户指南----第5章.@PathParam
    RestEasy 用户指南----第4章.使用@Path @GET @POST 等
    Yii2.0----环境搭建
    Vue----创建脚手架项目
    Vue----源码学习-基本实现原理(数据代理、模板编译、数据绑定的实现、双向数据绑定)
    Django框架学习----视图与模板(最新文章实现)
    Django框架学习----视图与模板(分页功能)
    shiro之自定义realm
    shiro之jdbcRealm
    shiro之IniRealm
  • 原文地址:https://www.cnblogs.com/guanghe/p/6063050.html
Copyright © 2011-2022 走看看