zoukankan      html  css  js  c++  java
  • Struts2学习-拦截器2续

    定义拦截器有2种办法:
    1.实现Interceptor接口
    2.集成AbstractInterceptor抽象类

    一、方法1

    .....
    
    <struts>
        <package name="mypack" extends="struts-default">
    <!--i.    j.    k.-->
            <interceptors>
                <interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
                <interceptor-stack name="myStack">
                    <interceptor-ref name="loginInterceptor"></interceptor-ref>
                    <interceptor-ref name="defaultStack"></interceptor-ref>
                </interceptor-stack>
    
            </interceptors>
    
            <default-interceptor-ref name="myStack"/>
    
            <global-results>
                <result name="login" type="redirectAction">userAction_loginView</result>
            </global-results>
    
            <!--不写method,111默认就是execute-->
            <action name="indexAction" class="com.nf.action.IndexAction">
                <result name="success">/WEB-INF/jsp/index.jsp</result>
    
                <interceptor-ref name="myStack"></interceptor-ref>
                <!--
                <interceptor-ref name="loginInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
                -->
            </action>
    
            <action ......
            </action>
        </package>
    
    </struts>
    package com.nf.action;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;
    
    import java.util.Map;
    
    /*
    定义拦截器有2种办法:
    1.实现Interceptor接口
    2.集成AbstractInterceptor抽象类
     */
    public class LoginInterceptor implements Interceptor {
        private Map<String,Object> session = null;
        public void destroy() {
        }
        public void init() {
        }
        public String intercept(ActionInvocation actionInvocation) throws Exception {
            Object myAction = actionInvocation.getAction();
            if(myAction instanceof UserAction){
                System.out.println("你访问的Action是UserAction,不要校验Session,否则死循环");
                //放行
                return actionInvocation.invoke();
            }else{
                System.out.println("你访问的Action是:"+myAction);
            }
    
            session = ActionContext.getContext().getSession();
            Object user = session.get("user");
            if (user!=null){
                return actionInvocation.invoke();
            }else{
                return "login";
            }
        }
    }

    二、方法2

    package com.nf.action;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;
    import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
    
    import java.util.Map;
    
    /*
    定义拦截器有2种办法:
    1.实现Interceptor接口
    2.集成AbstractInterceptor抽象类
     */
    public class LoginInterceptor extends MethodFilterInterceptor {
        private Map<String, Object> session = null;
    
        protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
            session = ActionContext.getContext().getSession();
            Object user = session.get("user");
            if (user!=null){
                return actionInvocation.invoke();
            }else{
                return "login";
            }
        }
    }
    ......
    <struts>
        <package name="mypack" extends="struts-default">
    <!--i.    j.    k.-->
            <interceptors>
                <interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
                <interceptor-stack name="myStack">
                    <interceptor-ref name="loginInterceptor">
                        <!--excludeMethods需要生效的话,
                        自定义的拦截器,不能使用实现Interceptor接口,
                        而是extends MethodFilterInterceptor
                        -->
                        <param name="excludeMethods">loginView,login</param>
                    </interceptor-ref>
                    <interceptor-ref name="defaultStack"></interceptor-ref>
                </interceptor-stack>
    
            </interceptors>
    
            <default-interceptor-ref name="myStack"/>
    
            <global-results>
                <result name="login" type="redirectAction">userAction_loginView</result>
            </global-results>
    
            <!--不写method,111默认就是execute-->
            <action name="indexAction" class="com.nf.action.IndexAction">
                <result name="success">/WEB-INF/jsp/index.jsp</result>
    
                <!-- <interceptor-ref name="myStack"></interceptor-ref> -->
                <!--
                <interceptor-ref name="loginInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
                -->
            </action>
    
            <action name="therFunctionAction" class="com.nf.action.OtherFunctionAction">
                <!--不写name,222默认就是success-->
                <result>/WEB-INF/jsp/otherFunction.jsp</result>
            </action>
    
            <action.....
            </action>
        </package>
    
    </struts>

    地址;https://gitee.com/MuNianShi/user5.git

  • 相关阅读:
    Windows下安装并设置Redis
    OpenGL纹理上下颠倒翻转的三种解决办法
    如何计算android设备的屏幕物理尺寸
    VS2010中使用QtOpenGL出现 unresolved external symbol __imp__glClear@4 referenced in function之类的错误
    VC运行库版本不同导致链接.LIB静态库时发生重复定义问题的一个案例分析和总结
    7月份文章回顾
    NSIS脚本根据操作系统版本动态决定默认安装目录
    WinDriver的一些
    〔池化纲领〕也及链表
    多道程序设计〕协程构造
  • 原文地址:https://www.cnblogs.com/junhua4254/p/7645620.html
Copyright © 2011-2022 走看看