zoukankan      html  css  js  c++  java
  • Struts2中配置全局拦截器的方法

    在struts.xml中添加如下配置:

    <!-- 配置全局拦截器 -->

    <package name="all" extends="struts-default">
            <interceptors>

                <!-- 定义权限控制拦截器 -->
                <interceptor name="authority"
                    class="akai.cost.ms.base.AuthInterceptor" />

                <!-- 定义一个包含权限控制的拦截器栈 -->
                <interceptor-stack name="mydefault">
                    <interceptor-ref name="defaultStack" />
                    <interceptor-ref name="authority" />
                </interceptor-stack>
            </interceptors>

            <!-- 定义默认拦截器 -->
            <default-interceptor-ref name="mydefault" />
            <!-- 定义全局处理结果 -->
            <global-results>
                <!-- 逻辑名为login的结果,映射到/login.jsp页面 -->
                <result name="login">/login.jsp</result>
            </global-results>

        </package>

    使用方法:其他包继承这个包名就可以了

    <package name="abc" extends="all" namespace="/">


    :拦截器类

    [java] view plain copy
     print?
    1. package akai.cost.ms.base;  
    2.   
    3. import javax.servlet.http.HttpSession;  
    4.   
    5. import org.apache.struts2.ServletActionContext;  
    6.   
    7. import com.opensymphony.xwork2.Action;  
    8. import com.opensymphony.xwork2.ActionInvocation;  
    9. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
    10.   
    11. public class AuthInterceptor extends AbstractInterceptor{  
    12.   
    13.     @Override  
    14.     public String intercept(ActionInvocation invocation) throws Exception {  
    15.         HttpSession session = ServletActionContext.getRequest().getSession();  
    16.         String userName = (String)session.getAttribute("System_UserName");  
    17.         if(userName == "" || userName == null){//错误,回到登录界面  
    18.             return Action.LOGIN;  
    19.         }else{  
    20.             return invocation.invoke();  
    21.         }  
    22.     }  
    23.   
    24. }  
  • 相关阅读:
    Redis QPS测试
    go语言下载及安装
    企业级Docker镜像仓库Harbor部署与使用
    Linux格式化数据盘
    【一周一Q】如何快速复制有规律内容
    聊一聊职业能力之执行力
    面试那些事
    使用gitlab时候 fork仓库不会实时从主仓库更新解决方案
    从给定字符串中提取姓名
    测试Websocket建立通信,使用protobuf格式交换数据
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317887.html
Copyright © 2011-2022 走看看