struts2笔记 - 配置2011-01-30 blogjava Gay Bird与Struts 1.X不同,Struts2引入了WebWork的配置机制,在很大程度上提高了配置的灵活度。通过使用配置可以配置如下内容:
| 配置类型 | 配置文件 |
| Struts2 参数配置 | struts.properties |
| Struts2 Action 配置 | struts.xml |
| Struts2 Result 配置 | struts.xml |
| Struts2 Exception Handling 配置 | struts.xml |
| Struts2 Intercept 配置 | struts.xml |
| Struts2 多配置文件 | struts.xml |
struts.properties文件在WEB-INF/classes目录下存放。这个文件用来配置Struts2系统的一些基本规约,所有在struts.properties中配置的内容都可以在struts.xml中配置,或者web.xml中在struts2 filter中配置,例如:Struts.properties中的如下配置:struts.i18n.encoding=UTF-8相当于struts.xml中的如下配置:<constant name=“struts.i18n.encoding” value=“true” />相当于web.xml中的如下配置:
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>Package配置使用package可以将逻辑上相关的一组Action,Result,Intercepter等组件分为一组,Package有些想对象,可以继承其他的Package,也可以被其他package继承,甚至可以定义抽象的Package。Package的可以使用的属性:
| 属性 | 是否必须 | 说明 |
| name | 是 | Package的表示,为了让其他的package引用 |
| extends | 否 | 从哪个package集成行为 |
| namespace | 否 | 参考Namespace配置说明 @TODO Namespace配置说明 |
| abstract | 否 | 定义这个package为抽象的,这个package中不需要定义action |
由于struts.xml文件是自上而下解析的,所以被集成的package要放在集成package的前边。