本章我们会向你介绍struts.xml文件,讲解如何最优化你项目的struts.xml.
struts.xml文件
Struts2使用一个配置文件(struts.xml)来初始化它自身的资源.这些资源包括 :
l 拦截器可以对一个请求进行预处理和后期处理.
l Action类可以调用业务逻辑与数据访问的代码.
l 结果可以使用JSP,Velocity和FreeMarker模板来预览.
在运行时,程序仅有一个配置.在运行时之前配置可经由一个或多个XML文件定义,包括默认的struts.xml文件.有如下几个元素可以配置 : packages, namespaces, includes, actions, results, interceptors,exceptions.
struts.xml文件是框架的核心配置文件,它应该放在你的web程序的类路径中.Struts2配置文件的特性有 :
l struts.xml文件允许将一个大的struts.xml文件分解成几个小的struts.xml文件,如果需要,配置文件也可以被导入进来.下面是个例子 :
<struts> |
l 你甚至可以将struts-plugin.xml文件放入一个JAR里,它会被自动加载到程序中.这可以帮助程序员开发自配置的组件.
l 如果你想使用其它框架如Freemaker和Velocity模块,那么模板同样可以在类路径中得到加载.这可以让开发人员将整个模块打包进一个JAR文件.
Struts.xml文件的结构
在上一章我们开发并测试了Hello World程序.下面的就是上个例子的struts.xml样本
<?xml version="1.0" encoding="UTF-8" ?> |
struts.xml文件必须由Struts2文档类型定义(DTD)来确认
该DTD文件提供了struts.xml应该拥有的结构和元素的信息
这是Struts2.0的DTD :
<!-- Struts configuration DTD. Use the following DOCTYPE
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> --> <!ELEMENT struts (package|include|bean|constant)*> <!ELEMENT package (result-types?, interceptors?, default-interceptor-ref?, default-action-ref?, global-results?, global-exception-mappings?, action*)> <!ATTLIST package name CDATA #REQUIRED extends CDATA #IMPLIED namespace CDATA #IMPLIED abstract CDATA #IMPLIED externalReferenceResolver NMTOKEN #IMPLIED > <!ELEMENT result-types (result-type+)> <!ELEMENT result-type (param*)> <!ATTLIST result-type name CDATA #REQUIRED class CDATA #REQUIRED default (true|false) "false" > <!ELEMENT interceptors (interceptor|interceptor-stack)+> <!ELEMENT interceptor (param*)> <!ATTLIST interceptor name CDATA #REQUIRED class CDATA #REQUIRED > <!ELEMENT interceptor-stack (interceptor-ref+)> <!ATTLIST interceptor-stack name CDATA #REQUIRED > <!ELEMENT interceptor-ref (param*)> <!ATTLIST interceptor-ref name CDATA #REQUIRED > <!ELEMENT default-interceptor-ref (param*)> <!ATTLIST default-interceptor-ref name CDATA #REQUIRED > <!ELEMENT default-action-ref (param*)> <!ATTLIST default-action-ref name CDATA #REQUIRED > <!ELEMENT global-results (result+)> <!ELEMENT global-exception-mappings (exception-mapping+)> <!ELEMENT action (param|result|interceptor-ref|exception-mapping)*> <!ATTLIST action name CDATA #REQUIRED class CDATA #IMPLIED method CDATA #IMPLIED converter CDATA #IMPLIED > <!ELEMENT param (#PCDATA)> <!ATTLIST param name CDATA #REQUIRED > <!ELEMENT result (#PCDATA|param)*> <!ATTLIST result name CDATA #IMPLIED type CDATA #IMPLIED > <!ELEMENT exception-mapping (#PCDATA|param)*> <!ATTLIST exception-mapping name CDATA #IMPLIED exception CDATA #REQUIRED result CDATA #REQUIRED > <!ELEMENT include (#PCDATA)> <!ATTLIST include file CDATA #REQUIRED > <!ELEMENT bean (#PCDATA)> <!ATTLIST bean type CDATA #IMPLIED name CDATA #IMPLIED class CDATA #REQUIRED scope CDATA #IMPLIED static CDATA #IMPLIED optional CDATA #IMPLIED > <!ELEMENT constant (#PCDATA)> <!ATTLIST constant name CDATA #REQUIRED value CDATA #REQUIRED > |
如果你的程序在功能上不依赖于struts.xml,你可以将它从你的程序中彻底移除.还有一些配置方式可以作为你的选择,例如 : 注释, web.xml启动参数, 和交替的URL映射计划.即使如此,仍有些配置需要Struts2.xml的支持,像全局结果, 异常处理, 以及自定义拦截器栈.
探索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等的配置将其分类到单独的文件中 :
<struts> <include file="invoices-config.xml" /> </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> |
4. 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例子(struts.properties)
struts.devMode = true