zoukankan      html  css  js  c++  java
  • struts2拦截器

    struts拦截器

    图:

    1、拦截器是什么?

    分离关注:

    完成一个功能,可以写在一个类中,然后一个类中4个步骤,实现该类完成。

    我们可以将4个步骤写在4个类中,然后每一个类完成一部分功能,然后将其按顺序执行,就可以完成我们想要的功能。

    Struts2中的处理是通过过滤器完成的,struts2就使用了分离关注这个思想,它定义了多个过滤器,让每一个过滤器只完成一个功能或者一个需求,然后核心过滤器只需要调用所有的自定义过滤器即可。然后这些自定义的过滤器就是struts2的拦截器。

    2、拦截器和过滤器的区别

    相同点:

             都是起拦截作用

    不同点:

               使用范围:

                        过滤器是属于J2EE的范围,过滤器所有的web工程都可以使用

                        拦截器是属于struts2框架的,使用拦截器必须在使用struts2框架,拦截器是离不开struts2框架的

               完成功能:

                        以前的过滤器:拦截处理所有的功能

    在拥有拦截器的情况下:只拦截页面的请求资源

    拦截器:其他的所有功能都交给拦截器来处理

                                执行顺序:

                                         先执行过滤器,然后再执行拦截器。因为拦截器由struts特定过滤器调用

    3、理解struts-default.xml:

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    
        <!--  
            package:是struts2框架底层提供出来的
            name:用于让其他包来继承的
            abstract:设置为抽象包,内部不能有action
        -->
        <package name="struts-default" abstract="true">
            <result-types>
                <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
                <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
                <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
                <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
                <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
                <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
                <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
                <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
                <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
                <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
                <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
            </result-types>
    
            <!--  
                interceptors
                    interceptor:声明拦截器
                        name:拦截器的名称
                        class:拦截器对应类的完成路径
            -->
            <interceptors>
                <interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
                <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
                <interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
                <interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>
                <interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>
                <interceptor name="cookieProvider" class="org.apache.struts2.interceptor.CookieProviderInterceptor"/>
                <interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />
                <interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />
                <interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
                <interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>
                <interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>
                <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
                <interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>
                <interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>
                <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
                <interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>
                <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
                <interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>
                <interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>
                <interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>
                <interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>
                <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
                <interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>
                <interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>
                <interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>
                <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
                <interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>
                <interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />
                <interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />
                <interceptor name="datetime" class="org.apache.struts2.interceptor.DateTextFieldInterceptor" />
                <interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />
                <interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />
                <interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />
                <interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" />
                <interceptor name="deprecation" class="org.apache.struts2.interceptor.DeprecationInterceptor" />
    
                <!-- Basic stack -->
                <interceptor-stack name="basicStack">
                    <interceptor-ref name="exception"/>
                    <interceptor-ref name="servletConfig"/>
                    <interceptor-ref name="prepare"/>
                    <interceptor-ref name="checkbox"/>
                    <interceptor-ref name="datetime"/>
                    <interceptor-ref name="multiselect"/>
                    <interceptor-ref name="actionMappingParams"/>
                    <interceptor-ref name="params"/>
                    <interceptor-ref name="conversionError"/>
                    <interceptor-ref name="deprecation"/>
                </interceptor-stack>
    
                <!-- Sample file upload stack -->
                <interceptor-stack name="fileUploadStack">
                    <interceptor-ref name="fileUpload"/>
                    <interceptor-ref name="basicStack"/>
                </interceptor-stack>
    
              
                <!--  
                    interceptor-stack:声明拦截器栈
                        struts2通过使用拦截器栈来使用一些声明好的拦截器
                        在拦截器栈里面存放了一些上面声明好的拦截器
                        拦截器栈相当于一个list集合。执行的时候是按照存放的先后顺序来执行的
                -->
                
                <interceptor-stack name="defaultStack">
                    <interceptor-ref name="exception"/>
                    <interceptor-ref name="alias"/>
                    <interceptor-ref name="servletConfig"/>
                    <interceptor-ref name="i18n"/>
                    <interceptor-ref name="prepare"/>
                    <interceptor-ref name="chain"/>
                    <interceptor-ref name="scopedModelDriven"/>
                    <interceptor-ref name="modelDriven"/>
                    <interceptor-ref name="fileUpload"/>
                    <interceptor-ref name="checkbox"/>
                    <interceptor-ref name="datetime"/>
                    <interceptor-ref name="multiselect"/>
                    <interceptor-ref name="staticParams"/>
                    <interceptor-ref name="actionMappingParams"/>
                    <interceptor-ref name="params"/>
                    <interceptor-ref name="conversionError"/>
                    <interceptor-ref name="validation">
                        <param name="excludeMethods">input,back,cancel,browse</param>
                    </interceptor-ref>
                    <interceptor-ref name="workflow">
                        <param name="excludeMethods">input,back,cancel,browse</param>
                    </interceptor-ref>
                    <interceptor-ref name="debugging"/>
                    <interceptor-ref name="deprecation"/>
                </interceptor-stack>
    
           </interceptors>
    
            <!-- 配置在struts2框架运行时默认要执行的拦截器栈 -->
            <default-interceptor-ref name="defaultStack"/>
    
            <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
        </package>
    
    </struts>

    自问自答:

    为什么我们在struts.xml中定义的package都显式或者隐式继承struts-default?

      因为struts2的功能被分化了,是通过各个拦截器来实现的。而各个基础拦截器的声明与组成拦截器栈调用时在struts-default.xml里面配置的。如果不继承这文件,就没有办法使用struts2提供的所有拦截器。

    为什么我们的struts文件都必须命名为struts.xml并且在classpath下?

      你可以查看源码,可以发现取读的配置文件的路径是一个静态final变量,依次是struts-default.xml,struts-plugin.xml,struts.xml

     

  • 相关阅读:
    系统相对路径和绝对路径
    delphi TreeView 鼠标右击选择节点
    treeview得到鼠标右键点击的节点
    TREEVIEW右键菜单示例
    ORACLE
    监视oracle执行的SQL语句
    oracle数据库跟踪工具
    PL/SQL plsql Developer 14最新版注册码 (亲测可用)
    一个框架看懂优化算法之异同 SGD/AdaGrad/Adam
    Node.jsp配环境更新中)
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/5886301.html
Copyright © 2011-2022 走看看