更改Struts的配置文件struts.xml 默认目录路径到WEB-INF (转)
我的文件目录
-src
-WEB-INF
--classes
--lib
--struts-config-sale.xml
--struts-config-hr.xml
--struts.xml
--web.xml
在web.xml中加上
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
注意../struts.xml前面有两个点,表示上级目录,struts2默认查找的是/WEB-INF/classes下的配置文件,所以要返回上级目录去查找struts.xml
如果在struts.xm中的如果要包含其他struts的配置文件,也要更改目录
<struts>
...
<include file="../struts-config-sale.xml" />
<include file="../struts-config-hr.xml" />
</struts>
源代码:
private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";
private void init_TraditionalXmlConfigurations() {
String configPaths = initParams.get("config");
if (configPaths == null) {
configPaths = DEFAULT_CONFIGURATION_PATHS;
}
String[] files = configPaths.split("\\s*[,]\\s*");
for (String file : files) {
if (file.endsWith(".xml")) {
if ("xwork.xml".equals(file)) {
configurationManager.addConfigurationProvider(new XmlConfigurationProvider(file, false));
} else {
configurationManager.addConfigurationProvider(new StrutsXmlConfigurationProvider(file, false, servletContext));
}
} else {
throw new IllegalArgumentException("Invalid configuration file name");
}
}
}
所以上面的配置只是改变了读取配置文件的变量值,struts2始终还是从/WEB-INF/classes目录下开始搜索配置文件。这样配置就OK了,不需要修改其他的。

浙公网安备 33010602011771号