zoukankan      html  css  js  c++  java
  • struts2常量配置

         在实际应用中我们需要根据用户的需求来更改struts2的默认配置来满足需求,这就涉及到配置文件的加载。

          struts实际上属于一种过滤器,要查询过滤器的init方法来了解struts的配置加载。

    这一系列代码就是用来加载Struts的配置文件的。

    init_DefaultPropertise();    加载rog.apache.struts.default.properties 配置的是struts2的所有常量

    init_TraditionalXmlConfiguration();   加载 struts-default.xml   struts-plugin.xml    struts.xml  

    init_LegacyStrutsPropertise();    加载用户自定义struts.properties

    init_CustomerConfigurationProviders();    加载用户配置的提供对象

    init_FilterInitParameters();       加载web.xml

    init_AliaStandardObject();       加载标准对象

     根据以上代码我们可以得出配置文件的加载顺序如下:

    • default.properties
    • struts-default.xml
    • struts-plugin.xml
    • struts.xml      配置Action 及常量
    • struts.xml      配置常量
    • web.xml         配置核心过滤器及常量

       

            前三个配置文件是Struts2内部配置文件,我们无法更改,我们能操作的是 struts.xml    struts.properties    web.xml  这三个文件是有加载顺序的,后加载的配置文件中的常量会覆盖先加载的。

    package配置:

    package元素的常用属性
    属性 说明
    name

    必填属性,它指定该包的名字,此名字是该包被其他包引用的key

    namespace

    可选属性,该属性定义该包的命名空间

    默认的名称空间: namespace=""

    根名称空间: namespace="/"

    带名称的名称空间: namespace="/demo1"

     

    extends      可选属性,它指定该包继承自其他包。继承其他包,可以继承其他包中的Action定义,拦截器等,该属性值必须是另一个包的name属性值但通常设置为struts-default 具有struts2默认的功能
    abstract 可选属性,它指定该包是否是一个抽象包,抽象包中不能含有Action定义

     

     Action配置:

    action元素属性说明
    属性 说明
    name 必填属性,标识Action,指定了Action所处理的请求的URL,决定了访问路径
    class 可选属性,指定Action对应的Action类,对应Action的全路径
    method 可选属性,指定请求Action时调用的方法,默认是execute方法
    converter 可选属性,指定类型转换器的类

     Struts常量的配置:

     

    •  在struts.xml文件中使用<constant>元素配置常量
    • 在struts.properties 文件中配置常量
    • 在web.xml文件中通过<init-param>元素配置常量

    在struts.xml 文件中配置:

    1 <struts>
    2     <!-- 设置编码-->
    3     <constant name="struts.il8n.encoding" value="UTF-8"/>
    4     <!-- 设置开发模式-->
    5     <constant name="struts.devMode" value="true"/>
    6 <struts/>        

     

    在struts.properties 文件中配置:

    1 ##设置默认编码 
    2 struts.il8n.encoding=UTF-8
    3 ### 设置action请求扩展名为action或没有扩展名
    4 struts.action.extension=action
    5 ###设置不使用开发模式
    6 struts.devMode=false
    7 ###设置不开启动态方法调用
    8 struts.enable.DynamicMethodInvocation=false

    在web.xml文件中通过初始化参数配置常量(放在filter标签下)

     1   <!-- 配置Struts2的核心过滤器:前端控制器 -->
     2   <filter>
     3       <filter-name>struts2</filter-name>
     4       <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
     5   
     6     <!-- 通过init-param 元素配置 默认编码 -->
     7     <init-param>
     8     <param-name>struts.iln8.encoding</param-name>
     9     <param-value>UTF-8</param-value>
    10     </init-param>
    11  
    12   </filter>
    13   <filter-mapping>
    14       <filter-name>struts2</filter-name>
    15       <url-pattern>/*</url-pattern>
    16   </filter-mapping>

     

    用的比较多的常量:

      1.  struts.i18n.reload=true :    激活重新载入国际化文件的功能。
      2. struts.devMode=true:   激活开发模式,提供更全面地调试功能
      3. struts.configuration.xml.reload=true:   激活重新载入xml配置文件的功能,当文件被修改后,就不需要载入servlet容器中的整个web应用了
      4. struts.url.http.port=8080:   配置服务器运行的端口
      5. struts.objectFactory=spring:   把Struts2的类生成交给spring完成

           Struts2所支持的常量数量众多,在 struts-core-2.3.24.jar压缩文件的/org/apache/struts2/default.properties文件中有所有的常量默认值,读者可以通过查看该文件来了解Struts所支持的常量。

    在实际开发中我们更习惯使用struts.xml配置文件来修改常量(支持分模块开发)。

    分模块开发的配置:

    1 <struts>
    2 <!-- 包含了三个配置文件 -->
    3 <!-- 不指定路径默认在src下时的方式 -->
    4 <include file="struts-shop.xml"/>
    5 <include file="struts-user.xml"/>
    6 <!-- 配置文件在具体包中时的方式 -->
    7 <include file="cn/itcast/action/struts-produce.xml"/>
    8 </struts>

           需要注意的是,每一个被包含的配置文件都是标准的Struts配置文件,一样包含DTD信息。

     

     

     

  • 相关阅读:
    jmeter压测,json提取器的使用
    pycharm安装
    安装numpy+mkl
    Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
    windows上安装tensorflow时报错,“DLL load failed: 找不到指定的模块”的解决方式
    mapreduce 设置递归读取输入文件
    设置reduce Task数量
    kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.
    mahout RecommenderJob 参数含义
    idea 配置日志输出等级debug
  • 原文地址:https://www.cnblogs.com/the-wang/p/7609544.html
Copyright © 2011-2022 走看看