探索struts.xml

<struts>标签是struts.xml文件的根标签.它有可能包含如下标签 : package, include, bean和constant.

1.     Package标签 :

 

Package是一种将actions, results, result types, interceptors, 和interceptor-stacks分类到一个逻辑配置单元的方法.从概念上说Package还与对象有点相似,因为它们都可以被继承并且某些部分可以被"子Package"重写.

 

<package/>标签用来将那些分配常见属性的配置归为一类,这些配置如拦截器栈或URL命名空间.它可能同样有利于有组织的分解一些功能,或许更大程度上分解为不同的配置文件.

 

package元素有一个必须的属性name,name对以后package的引用起主要作用.extend属性是可选的,这可以让一个package继承另一个或更多的package的配置,包括所有的拦截器,拦截器栈和Action配置.

 

 

注意配置文件是按顺序处理的,所以一个package应该定义在继承它的package之前.

 

可选属性abstract建立了一个基础的package可以忽略Action配置.

 

 

 

Attribute

Required

Description

name

yes

key for other packages to reference

extends

no

inherits package behavior of the package it extends

namespace

no

provides a mapping from the URL to the package.

abstract

no

declares package to be abstract (no action configurations required in package)

 

1)      name : package的name是唯一的

 

2)      extends 该package要继承的package的名字,在新的命名空间下,可以使用所有继承自另外一个package的配置信息(包括Action配置).

 

3)      namespace 命名空间提供了一个从URL到package的映射,也就是说,对于两个不同的package来说,namespace属性定义为pack1和pack2,URL可能就像是这样 : webApp/pack1/my.action和webApp/pack2/my.action

 

4)      abstract 如果该属性值是true,那么package就成了一个真正的配置分组,而且Action配置无法通过package的名字进行访问.确认你继承了正确的父package是很重要的,只有这样,预配置才会生效.

 

 

2.include标签

<include/>标签是用来模块化Struts2程序的,这需要包含其它的配置文件.它只包含一个属性,那就是提供要包含的.xml文件的名字.这个文件与struts.xml有着非常相似的结构.例如,要分割一个财务程序的配置文件,你可能要选择invoices(发票), admin,  report等的配置将其分类到单独的文件中 :

1 <struts>  
2 <include file="invoices-config.xml" />  
3 <include file="admin-config.xml" />  
4 <include file="reports-config.xml" />  
5 </struts>  

当引入文件的时候,顺序很重要.被引入文件的信息会在include标签放置文件的地方生效.

还有些文件会被暗中引入进来,那就是struts-default.xml文件和struts-plugin.xml文件.它们都包含了结果类型,拦截器,拦截器栈,package的默认配置信息,还有web程序执行环境的配置信息(这也同样可以在struts.properties文件中配置).不同的是,struts-default.xml提供了Struts2的核心配置,struts-plugin.xml提供了流行插件的配置.每一个JAR插件文件都应该包含一个struts-plugin.xml文件,这些都会在启动时被加载.

 

3.bean标签

大多数程序都不需要继承bean配置.bean元素需要class属性指定要被创建或者使用的java类.一个bean可能 :

1.     被框架的容器创建然后注入到框架的内部对象,或者

2.     使用值注入到自身的静态方法中.

 

第一种用法,对象注入,一般来说都伴随着type属性,告诉容器这个对象实现了哪个接口.

 

第二种用法,值注入,对于允许不是容器创建的对象去接收框架常量来说是不错的.对象使用值注入必须定义静态属性

 

Attribute

Required

Description

class

yes

the name of the bean class

type

no

the primary Java interface this class implements

name

no

the unique name of this bean; must be unique among other beans that specify the same type

scope

no

the scope of the bean; must be either default, singleton, request, session, thread

static

no

whether to inject static methods or not; shouldn't be true when the type is specified

optional

no

whether the bean is optional or not

 

Bean的例子(struts.xml)

<struts>  
  
  <bean type="roseindia.net.ObjectFactory" name="factory" class="roseindia.net.MyObjectFactory" />  
    
  ...   
  
</struts>  

1.     Constant标签

constants有两个关键作用 :

1.      用来重写一些设置,如文件上传大小的最大值或者struts架构是不是在devMode(开发模式)

2.      在多个给定类型的实现中指定应该选择哪一个Bean

 

Constants能在多个文件中声明.默认的,Constants会按下面的顺序搜索,允许后面的文件重写前面的.

  • struts-default.xml
  • struts-plugin.xml
  • struts.xml
  • struts.properties
  • web.xml

struts.properties提供了对WebWork的向后兼容性.在struts.properties中,每一条输入都会被当作常量.在web.xml文件中,任何FilterDispatcher的初始化参数都会被当作常量被加载.

 

在不同的XML变量中,constant元素包含两个必要的属性 : name和value

 

Attribute

Required

Description

name

yes

the name of the constant

value

yes

the value of the constant

 

Constant例子(struts.xml)

<struts>  
  
  <constant name="struts.devMode" value="true" />  
  
  ...   
  
</struts>  

Constant例子(struts.properties)

struts.devMode = true

 posted on 2012-05-22 17:13  蔡傑儒  阅读(153)  评论(0编辑  收藏  举报