Struts2学习笔记四:struts.xml

来源:互联网 发布:网络机房空调 编辑:程序博客网 时间:2024/06/05 00:07

struts.xml是Struts2框架的核心配置文件。该文件主要负责管理Struts2框架中的Action。如果有多个类似的配置文件,可以在struts.xml中使用<include>标签将其它配置文件包含起来,格式为:<include file="struts1.xml" />。struts.xml放在src目录下即可。应用程序会自动加载一个名为 struts-default.xml文件,这个文件就加载了许多定义好的类和插件等配置。

struts.xml的配置元素如下:

  1. 配置Bean
    • 作用
      1. 创建Bean实例,并作为框架的内部对象使用
      2. 对Bean包含的静态方法进行注入
    • 属性
      1. class(必选):Bean的类名
      2. type(可选):Bean实现的接口
      3. name(可选):Bean实例的名字,必须唯一
      4. scope(可选):Bean实例的作用域,值必须是default、singleton、request、session或thread其中之一
      5. static(可选):指定Bean是否使用静态方法注入。当指定type属性时,该属性值不能为true。
      6. optional(可选):是否为一个可选的Bean
    •  格式:<bean class="" name="" optional="" scope="" static="" />

  2. 配置常量
    •   常量配置方法
      1. 在struts.properties文件中配置常量
      2. 在struts.xml文件中配置常量,struts2推荐使用此方式
      3. 在web.xml中配置常量
    • 常量的读取顺序
      1. struts-default.xml
      2. struts-plugin.xml
      3. struts.xml
      4. struts.properties
      5. web.xml
    • 如果在多个文件中配置同一个常量,则后一个配置文件中的常量会覆盖前一个配置文件中的同名常量的值

  3. 配置包
    • 属性
      1. name(必选):包的名字
      2. extends(可选):指定该包继承自哪个包。如果该包继承了其他包,就可以继承包中的Action、拦截器等
      3. namespace(可选):包的命名空间
        • 命名空间名必须以斜线(/)开头
        • Struts2不会对命名空间分层查找
      4. abstract(可选):指定当前包是否为一个抽象包。抽象包不能包含Action
      5. externalReferenceResolver(可选):暂未使用过

  4.  配置拦截器

    <!-- 配置拦截器 --><interceptors><!-- 拦截器 --><interceptor name="" class="" /><!-- 拦截器栈,即多个不同拦截器的集合 --><interceptor-stack name=""><interceptor-ref name="" /><interceptor-ref name="" /></interceptor-stack></interceptors>

0 0
原创粉丝点击