六、Struts2的配置文件

六、Struts2的配置文件

1、default.properties:在struts2-core-**.jar的org.apache.struts包中

关于Struts2一些常量配置(框架内部)

  1. struts-default.xml:在struts2-core-**.jar中。(框架内部)

    定义了一些bean;

    定义了一个抽象的包:struts-default

    定义了一些结果视图

    定义了一些拦截器和拦截器小组

  2. struts-plugin.xml :在struts2的第三方插件中(插件内部)
  3. struts.xml:用户自己编写的(重点)
  4. struts.properties:用户自己编写的(类路径中。不需要)
  5. web.xml:struts2的一些配置可以写在这(Web应用中。不需要)

    结论:1~6:后面的配置文件内容,会覆盖前面的。

    比如:在struts.xml中覆盖default.properties中的内容,可以这么办:

    <constant name="struts.action.extension" value="do"></constant>

    比如:在web.xml中把访问的扩展名改为itheima

    <filter>

        <filter-name>struts2</filter-name>

        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

        <init-param>

            <param-name>struts.action.extension</param-name>

            <param-value>itheima</param-value>

        </init-param>

    </filter>

     

    6.1struts.xml中的package元素:

    package:方便管理动作

    属性:

    name:配置文件中唯一

    namespace:取值一般以"/"开头,它与动作名称组成完成的访问路径。默认值是""。

    extends:父包名称。把父包中定义的内容完全继承下来。一般情况下,在struts2开发中需要继承(直接或间接)struts-default(struts-default.xml)

    abstract:抽象包。抽象的东西就是设计被别人继承的。没有任何action子元素的packkage就可以定义为抽象的。

    package的namespace和动作访问专题:(小难点)

     

    区分:namespace="/",实实在在的一个名称空间。绝对不是默认的:namespace=""或者不写

     

    6.2struts.xml中action元素的配置

    作用:定义动作类

    属性:

  • class:动作类的全名。默认值是com.opensymphony.xwork2.ActionSupport

    因为struts-default.xml有指定。

    <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />

  • method:指定动作类中的哪个方法。默认值是execute

 

小知识:

可以在自己的package中通过以下标签,指定默认的动作类

<default-class-ref class="com.itheima.action.HelloAction"></default-class-ref>

 

6.3strutx.xml中的result元素的默认配置

作用:定义动作类执行完毕后转向的结果视图

属性:

name:结果视图的名称。与当前的动作类的动作方法返回值对应。默认值是success

type:结果视图的类型。默认是dispatcher.(转发)

来自:struts-default.xml中的结果视图的定义。

 

6.4struts2的一些常量含义及配置

 

覆盖这些框架的默认值:struts.xml

<constant name="struts.action.extension" value="do"></constant>

 

  • struts.i18n.encoding=UTF-8    框架使用的编码
  • struts.action.extension=action,,    动作的访问后缀。多个可以使用逗号分隔
  • struts.serve.static.browserCache=true    指示浏览器是否缓存资源
  • (原理:三个响应消息头:Expires Cache-Control Pragma
  • struts.configuration.xml.reload = true 是否在更改了struts.xml后自动重新加载。开发阶段有用。
  • struts.devMode = false        是否是开发模式。开发阶段建议为true。如果为truestruts.configuration.xml.reload就会为true
  • struts.ui.theme=xhtml    指定页面用的主题(struts标签时介绍)
  • struts.objectFactory = spring    默认情况,Action都是struts2框架给我们创建的(ObjectFactory,实例工厂)。与Spring框架整合时用。
  • struts.enable.DynamicMethodInvocation = false 是否允许DMI(动态方法调用:Dynamic Method Invocation)。
  • struts.multipart.maxSize=2097152 指定文件上传时的大小限制。2M

6.4配置文件分模块化

引入外部的struts.xml配置文件(xml声明和根元素一致)

<include file="user.xml"></include>

原文地址:https://www.cnblogs.com/Prozhu/p/5886353.html