首页 > 其他 > 详细

struts拦截器

时间:2017-03-22 12:37:02      阅读:178      评论:0      收藏:0      [点我收藏+]

新建拦截器需继承一个拦截器的实现类

public class LoginInterceptor extends AbstractInterceptor {
    
    //ActionInvocation描述的是整个strutsAction类运行过程,它封装了一次strutsAction执行过程中的所有信息
    public String intercept(ActionInvocation invocation) throws Exception {
        //如果是主页跳转到登录页面,放行
        String action = invocation.getProxy().getActionName();
        if(action.equals("pages_login")){
            return invocation.invoke();
        }
        
        //获取本次操作的内容
        String actionName = invocation.getProxy().getAction().getClass().getName();
        String methodName = invocation.getProxy().getMethod();
        String totalName = actionName + "." + methodName;
        //校验本次操作是否为登录
        
        //如果当前操作是登录功能,放行
        if("cn.itcast.invoice.auth.emp.web.EmpAction.login".equals(totalName)){
            return invocation.invoke();
        }
        // 1.先获取当前登录用户的登录信息
        // 从session中获取loginEm
        EmpModel loginEm = (EmpModel) ActionContext.getContext().getSession().get("loginEm");
        // 2.判断登录信息是否为null
        if(loginEm == null){
            // 2.1 如果为空,则用户没有登录,跳转到登录页面
            return "noLogin";
        }
        // 2.2如果不为空,放行
        return invocation.invoke();
    }

}

 

在struts2的struts.xml文件中配置:

<!-- 拦截器配置必须出现在配置的上方 -->
    <interceptors>
        <!-- 配置自定义拦截器 -->
        <interceptor name="loginInterceptor" class="cn.itcast.invoice.util.interceptor.LoginInterceptor"></interceptor>
        <interceptor name="exceptionInterceptor" class="cn.itcast.invoice.util.interceptor.ExceptionInterceptor"/>
        <interceptor name="authInterceptor" class="cn.itcast.invoice.util.interceptor.AuthInterceptor"/>
        <!-- 需要既使用自定义的拦截器,还要使用struts默认的拦截器,将两者结合在一起 -->
        <!-- struts中提供了拦截器栈,用于组合拦截器 -->
        <interceptor-stack name="systemStack">
            <!-- 定义所有使用的拦截器 -->
            <interceptor-ref name="exceptionInterceptor"/>
            <interceptor-ref name="loginInterceptor"/>
            <interceptor-ref name="authInterceptor"/>
            <interceptor-ref name="defaultStack"/>
        </interceptor-stack>
    </interceptors>
    
    <!-- 开启默认拦截器设置 -->
      <default-interceptor-ref name="systemStack"/> 

struts拦截器

原文:http://www.cnblogs.com/xiaozuo-2016/p/6599043.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!