Struts 2的配置文件在具体项目实践中一般都需要改变默认设置。
修改 struts.xml 的位置
Struts 2的配置文件 struts.xml 默认位置是:
{webroot}/WEB-INF/class/struts.xml
但这个位置可以在 web.xml 中配置 Struts 2过滤器时修改:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,META-INF/config/struts/struts.xml</param-value>
</init-param>
</filter>
即通过对 filter 添加名为 config 的初始参数来配置。
其中 struts-default.xml 在 struts2-core-2.3.*.jar 中。
拆分 struts.xml
如果将所有的 Action 都配置到 struts.xml 一个文件中,不利于项目维护,可以分拆。
在 struts.xml 如下配置:
<include file="META-INF/config/struts/sysadm/dict-struts.xml"></include>
<include file="META-INF/config/struts/sysadm/navibar-struts.xml"></include>
以上内容放在 <struts></struts>
块中。
拆分的实例参考也可以在 struts2-blank 这个官方自带的示例中找到。
一般说 struts.xml 配置常量等全局性内容,分拆後的配置文件对应不同业务模块的内容。struts.xml 和分拆後的配置文件,格式都是一样的。