zoukankan      html  css  js  c++  java
  • 【Struts2三】拦截器

    拦截器:就是在訪问action之前。对其进行拦截!能够在拦截器中做一些逻辑的处理!

    比方权限验证。没有权限就不给予訪问!

    拦截器等效于servlet中的过滤器!



    使用拦截器步骤:
    1.定义自己的拦截器:
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;

    /**
     * 定义自己的拦截器,须要实现Intercept接口!
     *
     */
    public class MyInterceptor implements Interceptor{

         public void destroy() {
              
         }

         public void init() {
              
         }
         /**
          * 重写intercept方法,在该方法中实现自己的拦截逻辑!
          * 调用invocation.invoke()方法放行action!
          */
         public String intercept(ActionInvocation invocation) throws Exception {
              System. out.println("图片上传" );
               return invocation.invoke();
    //        return null;//不运行action
         }

    }


    2.在struts-interceptor.xml的配置文件里声明:
      须要在struts.xml中包括上述配置文件!


    struts-interceptor.xml:
    <?xml version= "1.0" encoding ="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
         <package name="interceptor" namespace="/" extends="struts-default" >
               <!-- 声明拦截器,拦截器声明应该位于action之前-->
               <interceptors>
                   <!--声明自定义的拦截器 -->
                   <interceptor name= "imageInterceptor"
                        class= "cn.itheima03.struts2.interceptor.MyInterceptor" ></interceptor>

                   <!-- 声明拦截器栈 -->
                   <interceptor-stack name= "myInterceptor">
                        <interceptor-ref name="imageInterceptor" ></interceptor-ref>
                        <interceptor-ref name="defaultStack" ></interceptor-ref>
                   </interceptor-stack>
               </interceptors>

               <!-- 改动默认的拦截器栈 -->
               <default-interceptor-ref name="myInterceptor" ></default-interceptor-ref>

               <!--声明action,在运行action之前。会先运行拦截器中的方法  -->
               <action name= "interceptorAction_*" method ="{1}"
                    class= "cn.itheima03.struts2.interceptor.InterceptorTestAction" >
                   <result>
                       index.jsp
                   </result>
               </action>
         </package >

    </struts>

    3.定义action:
    import com.opensymphony.xwork2.ActionSupport;

    public class InterceptorTestAction extends ActionSupport{
         public String interceptor(){
              System. out.println("interceptor" );
               return SUCCESS ;
         }
    }



  • 相关阅读:
    WrapPanel虚拟化
    关于Windows执行cmd命令的坑之一
    C# .Net 获取当前电脑上的可移动磁盘
    关于WPF中资源字典的内存优化
    WPF获取ListBox、ListView之类列表的ScrollViewer实例
    C#事件与委托 yangan
    系统自动登录设置(适用所有操作系统) yangan
    Oracel小知识记录 yangan
    使用avaScript获取Url中的指定参数值 yangan
    Win7下破解ArcGIS 9.3方法 yangan
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8340585.html
Copyright © 2011-2022 走看看