• 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

  • 相关阅读:
    PowerShell 笔记 对象
    PowerShell 笔记 管道进阶
    PowerShell 笔记 基础篇
    vue2 jsx 渲染函数
    浏览器控制台打开影子dom
    EMQX之WebHook及其案例实现
    Docker方式部署V2XHub
    V2XHub,车路协同云控平台
    SVN工作笔记 设置SVN提交日志信息模板
    【Windows】(USB热点连接)使用手机给主机提供热点连网
  • 原文地址:https://www.cnblogs.com/junhua4254/p/7645620.html
Copyright © 2020-2023  润新知