• Struts2学习笔记(二)——配置详解


    1、Struts2配置文件加载顺序:

    • default.properties(默认常量配置)
    • struts-default.xml(默认配置文件,主要配置bean和拦截器)
    • struts-plugin.xml(配置插件)
    • struts.xml(配置action或者常量等)
    • struts.properties(常量配置)
    • web.xml(配置JavaEE,如:监听器和过滤器)

    2、Struts2配置文件详解

    1)default.properties

    default.properties是Struts2的全局常量配置文件,default.default.properties的默认配置:

     1 //Struts2默认的编码类型是UTF-8
     2 struts.i18n.encoding=UTF-8
     3 //指定jakarta为Struts的默认文件上传包,即默认使用apache的fileupload组件。
     4 struts.multipart.parser=jakarta
     5 //上传文件的最大字节数
     6 struts.multipart.maxSize=2097152
     7 //表单提交或者url请求时地址的后缀,如果需要指定多个请求后缀,则以英文逗号隔开
     8 struts.action.extension=action,,
     9 //Struts中action创建都是由对应的工厂创建。
    10 struts.objectFactory = spring
    11 //指定spring框架的自动装配类型,默认值为name,即默认根据bean的name属性自动装配
    12 struts.objectFactory.spring.autoWire = name
    13 //动态方法调用
    14 struts.enable.DynamicMethodInvocation = true
    15 //是否为开发模式
    16 struts.devMode = false
    17 //对于开发来讲还是必将重要的。设置为true时,在每次请求时,资源包就会被重载。
    18 struts.i18n.reload=false
    19 //设置为true时,我们每次修改struts.xml文件后,框架会自动加载这个文件。
    20 struts.configuration.xml.reload=false
    21 //页面使用静态方法。通过ognl标签调用值栈action中的方法。
    22 struts.ognl.allowStaticMethodAccess=false

    default.properties是不能直接修改的,我们如果要修改,有两种方式:

    • 在src下创建struts.properties
      • 例如:struts.enable.DynamicMethodInvocation = true
    • 在struts.xml中配置(推荐使用)
      • 例如:<constant name="struts.devMode" value=”true” />

    2)struts-default.xml

    struts-default.xml文件是struts2框架默认加载的配置文件。它定义struts2一些核心的bean和拦截器。这些拦截器是以key-value对的形式配置在struts-default.xml中,其中name是拦截器名字,就是后面使用该拦截器的引用点,value则指定拦截器的实现类。

    <?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:声明结果类型
                    * name:结果类型的名称
                    * class:结果类型对应类的完整路径
                    * default:设置其为默认,true是默认
             -->
            <result-types>
                <!-- 转发到action -->
                <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
                <!-- 转发到jsp -->
                <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"/>
                <!-- 重定向到jsp -->
                <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
                <!-- 重定向到action -->                                              
                <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-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="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="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="expessionInterceptor" class="cn.itcast.aop.ExpessionInterceptor"></interceptor>
    
                <!-- 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="multiselect"/>
                    <interceptor-ref name="actionMappingParams"/>
                    <interceptor-ref name="params">
                        <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
                    </interceptor-ref>
                    <interceptor-ref name="conversionError"/>
                </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">
                        <param name="maximumSize">20971520</param>
                        <param name="allowedTypes">text/plain</param>
                        <param name="allowedExtensions">.txt</param>
                    </interceptor-ref>
                    <interceptor-ref name="checkbox"/>
                    <interceptor-ref name="multiselect"/>
                    <interceptor-ref name="staticParams"/>
                    <interceptor-ref name="actionMappingParams"/>
                    <interceptor-ref name="params">
                        <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
                    </interceptor-ref>
                    <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="expessionInterceptor"/>
                    
                </interceptor-stack>
    
           </interceptors>
    
            <!-- 配置在struts2框架运行时,默认要执行的是哪个拦截器栈,defaultStack -->
            <default-interceptor-ref name="defaultStack"/>
    
            <!-- 配置在struts2框架运行时,如果没有为action指定class的话,默认要执行的class的类名 -->
            <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
        </package>
    
    </struts>

    3)struts-plugin.xml

    如果不是开发插件的话,是不需要编写这个配置文件的,一般是使用插件。

    4)struts.xml

    struts.xml 为Struts 2的核心配置文件,主要负责管理应用中的Action映射,以及该Action包含的Result定义等。

    • 常量配置
    • 包配置
    • include包含配置
    • 拦截器配置
    • 全局result设置
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 <struts>
     6     <!-- 
     7         1、constant:配置常量
     8             * name:指定的是struts2框架底层提供的default.properties资源文件中配置的"常量"
     9             * value:指定的是配置常量的值
    10             * 在struts.xml文件中,配置的常量的值会覆盖底层提供的default.properties资源文件中配置的常量的值
    11         
    12         * 配置struts2框架的页面中请求连接的后缀名,如果指定多个的话,用","隔开
    13         * 如果在struts.xml中和struts.properties资源文件中同时进行配置,struts.properties的配置起作用
    14         * 因为常量可以在多个配置文件中进行定义,所以我们需要了解下struts2加载常量的搜索顺序:
    15             1 struts-default.xml
    16             2 struts-plugin.xml
    17             3 struts.xml
    18             4 struts.properties(自己创建)
    19             5 web.xml
    20      -->
    21     <constant name="struts.devMode" value="true"></constant>
    22         
    23     <!-- 
    24         配置所有资源文件,省略后缀名,如果配置多个资源文件时,用","隔开。不仅是国际化资源文件
    25         * 类型转换器的错误提示资源文件
    26         * 国际化资源文件
    27         * 上传文件的错误提示信息资源文件
    28      -->
    29     <constant name="struts.custom.i18n.resources" 
    30             value="cn.sunny.converter.converter,
    31                     cn.sunny.i18n.resources,
    32                     cn.sunny.upload.fileuploadmessage">
    33     </constant>
    34     
    35     <!-- 配置文件上传的总大小 -->
    36     <constant name="struts.multipart.maxSize" value="2097152000"></constant>
    37     
    38     
    39     <!--
    40         2、包配置
    41         package:包
    42           * name:包名,唯一的,必选项
    43           * namespace:命名空间,唯一的,相当于房间号。可选项,省略情况下是"/"。页面中请求连接的前半部分
    44           * extends:继承其他package
    45             * extends="struts-default":struts2框架底层提供的核心包struts2-core-2.3.3.jar下的struts-default.xml文件
    46     -->
    47     <package name="default" namespace="/" extends="struts-default">
    48         <!-- 
    49             action:
    50                 * name:对应页面中请求连接的后面半部分
    51                 * class:对应要执行的类的完整路径
    52                 * method:要执行的方法名称,默认为execute方法
    53          -->
    54         <action name="testAction" class="com.sunny.action.TestAction" method="test">
    55             <!-- 
    56                 result:结果类型
    57                     * name:对应的是执行的类的方法的返回值
    58                     * 后半部分的文本内容:要转向到的页面
    59              -->
    60             <result name="success">/success.jsp</result>
    61         </action>
    62     </package>
    63     
    64     <!-- 
    65         3、引入自定义配置文件 
    66         include节点是struts2中组件化的方式,
    67         可以将每个功能模块独立到一个xml配置文件中,
    68         然后用include节点引用 
    69     -->
    70     <include file="struts_user.xml"/>
    71 
    72     <!--4、拦截器配置,后面讲拦截器的时候还会具体讲解-->
    73     <interceptors>
    74         <!-- 定义拦截器 
    75             name:拦截器名称
    76             class:拦截器类路径
    77          -->
    78         <interceptor name="logger" class="com.sunny.logger"/>
    79         <!-- 定义拦截器栈 -->
    80         <interceptor-stack name="mystack">
    81             <interceptor-ref name="defaultStack"/>
    82             <interceptor-ref name="logger"/>
    83         </interceptor-stack>
    84     </interceptors>
    85     <!-- 配置修改struts2框架运行时,默认执行的是自定义拦截器栈 -->
    86     <default-interceptor-ref name="expessionStack" />
    87 
    88     <!-- 5、全局results配置 -->
    89     <global-results>
    90         <result name="input">/error.jsp</result>
    91     </global-results>
    92 </struts>

    5)struts.properties

    struts.properties文件是一个标准的Properties文件,该文件包含了系列的key-value对象,每个key就是一个Struts 2属性,该key对应的value就是一个Struts 2属性值。

    Struts2在default.properties文件中给出了所有属性的列表,并对其中一些属性设置了默认值,如果想改变这些默认值或者给那些没有在default.properties文件中设置值的属性设置值,则可以使用struts.properties,如果设置了struts.properties文件,那么在该文件中的属性会覆盖default.properties文件中的属性,struts.properties的使用方法和default.properties一样。

    struts.properties一般放在src目录下。

    6)web.xml

    任何MVC框架都需要与Web应用整合需要助于web.xml文件,只有配置在web.xml文件中Servlet才会被应用加载。通常,所有的MVC框架都需要Web应用加载一个核心控制器,对于Struts2框架而言,需要加载StrutsPrepareAndExecuteFilter,只要Web应用负责加载StrutsPrepareAndExecuteFilter,StrutsPrepareAndExecuteFilter将会加载Struts2框架。因为Struts2将核心控制器设计成Filter,而不是一个普通Servlet。故为了让Web应用加载StrutsPrepareAndExecuteFilter,只需要在web.xml文件中配置StrutsPrepareAndExecuteFilter即可。此外web.xml依然可以配置JavaEE信息,比如初始化信息,Servlet、Filter等。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
     5 
     6     <display-name>Struts Blank</display-name>
     7 
     8     <filter>
     9         <filter-name>struts2</filter-name>
    10         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    11     </filter>
    12 
    13     <filter-mapping>
    14         <filter-name>struts2</filter-name>
    15         <url-pattern>/*</url-pattern>
    16     </filter-mapping>
    17 
    18 </web-app>
  • 相关阅读:
    给有C或C++基础的Python入门 :Python Crash Course 4 操作列表 4.4 -- 4.5
    给有C或C++基础的Python入门 :Python Crash Course 4 操作列表 4.1--4.3
    因数表进阶:1--x的因数和
    【python】接口自动化初识第三站
    【python】接口自动化初识第二站
    【python】接口自动化初识第一站
    【pycharm】无法自动补齐代码
    【python】UI自动化框架搭建
    【jmeter】B接口通过正则表达式引用A接口的参数
    测试流程测试方法
  • 原文地址:https://www.cnblogs.com/xiaolongSunny/p/4629834.html
Copyright © 2020-2023  润新知