zoukankan      html  css  js  c++  java
  • struts2学习4--拦截器

    一、最简单的拦截器

    1)自定义

    拦截器类:

    public class LoggerInterceptor  extends AbstractInterceptor{
        @Override
        public String intercept(ActionInvocation invocation) throws Exception {
            // TODO Auto-generated method stub
            System.out.println("记录日志............");
            return invocation.invoke();
        }
    }

    2)配置:

        <package name="User" namespace="/user" extends="struts-default">
            <!-- 包要用的拦截器 -->
             <interceptors>
             <interceptor name="logger"    class="com.meetcomet.LoggerInterceptor"/>
             <interceptor name="authority" class="com.meetcomet.AuthorityInterceptor"/>
             </interceptors>
            <action name="User*" class="com.meetcomet.action.UserAction" method="{1}" >
                <result name="success">/welcome.jsp</result>
                <interceptor-ref name="logger"/>
             </action>
        </package>


    二、拦截器栈

    1)拦截器类(同一)

    2)配置

        <package name="User" namespace="/user" extends="struts-default">
            <!-- 包要用的拦截器 -->
             <interceptors>
                <interceptor name="logger"    class="com.meetcomet.LoggerInterceptor"/>
                <interceptor name="authority" class="com.meetcomet.AuthorityInterceptor"/>
                <interceptor-stack name="mystack" >
                   <interceptor-ref name="authority"/>
                   <interceptor-ref name="logger"/>
                </interceptor-stack>
             </interceptors>
            <action name="User*" class="com.meetcomet.action.UserAction" method="{1}" >
                <result name="success">/welcome.jsp</result>
                <interceptor-ref name="mystack"/>
             </action>
        </package>

    按栈顺次运行。

    三、拦截器的例子

    1)用接口方法实现(可用类可用接口)的时间类,(其实可以直接用timer拦截器,见后)

    public class MyLoggingInterceptor implements Interceptor{
     
        private static final long serialVersionUID = 1L;
     
        public String intercept(ActionInvocation invocation) throws Exception {
     
            String className = invocation.getAction().getClass().getName();
            long startTime = System.currentTimeMillis();
            System.out.println("Before calling action: " + className);
     
            String result = invocation.invoke();
     
            long endTime = System.currentTimeMillis();
            System.out.println("After calling action: " + className
                    + " Time taken: " + (endTime - startTime) + " ms");
     
            return result;
        }
     
        public void destroy() {
            System.out.println("Destroying MyLoggingInterceptor...");
        }
        public void init() {
            System.out.println("Initializing MyLoggingInterceptor...");
        }
    }

     2)权限管理(用AbstractInterceptor实现)

    public class AuthorityInterceptor extends AbstractInterceptor {
        @Override
        public String intercept(ActionInvocation invocation) throws Exception {
            // TODO Auto-generated method stub
            ActionContext ctx = invocation.getInvocationContext();
            Map mySession = ctx.getSession();
            String role=(String)mySession.get("ROLE");
            if (null!=role && role.equals("admin"))
            {
                System.out.println("调用授权........");
                return invocation.invoke();
                                        
            }
            ctx.put("tip", "你还没有登录");
            return Action.LOGIN;
        }
    }

     四、系统拦截器

    系统提供了很多拦截器。比如 timer,会把action的运行时间打印在后台

            <action name="User*" class="com.meetcomet.action.UserAction" method="{1}" >
                <result name="success">/welcome.jsp</result>
                <interceptor-ref name="mystack"/>
                <interceptor-ref name="timer"/>
             </action>

    五、拦截方法

    1)拦截类 继承MethodFilterInterceptor类

    public class MethodInterceptor extends MethodFilterInterceptor {
        @Override
        protected String doIntercept(ActionInvocation invocation) throws Exception {
            // TODO Auto-generated method stub
            System.out.println("拦截方法 ------");
            return invocation.invoke();
        }
    }

    2)配置:
    add,del方法不用拦截。也可用includeMethod参数

            <action name="User*" class="com.meetcomet.action.UserAction" method="{1}" >
                <result name="success">/welcome.jsp</result>
                <interceptor-ref name="mystack"/>
                <interceptor-ref name="timer"/>
                <interceptor-ref name="method">
                    <param name="excludeMethods">add,del</param>
                </interceptor-ref>
             </action>
  • 相关阅读:
    我cnblogs的主题
    Scala Error: error while loading Suite, Scala signature Suite has wrong version expected: 5.0 found: 4.1 in Suite.class
    Spark之路 --- Scala用JFreeChart画图表实例
    Spark之路 --- Scala IDE Maven配置(使用开源中国的Maven库)和使用
    Spark之路 --- Windows Scala 开发环境安装配置
    epoll函数
    Linux网络编程目录
    函数wait和waitpid
    会话
    进程组
  • 原文地址:https://www.cnblogs.com/meetcomet/p/3406068.html
Copyright © 2011-2022 走看看