1.在服务器启动的时候加载资源:
struts-default.xml,struts-plugin.xml,struts.xml
具体实现代码:
package org.apache.struts2.dispatcher;
/**
* Provide list of default configuration files.
*/
private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";
通过调用下面的方法,加载三个文件字符串
private void init_TraditionalXmlConfigurations(){
//读取web.xml中的init参数config参数,从这儿也可以看出为什么在web.xml中配置时必须写config的原因
String configPaths = initParams.get("config");
//如果configPaths为空,则使用默认,如果用户自己指定,则必须要指定struts-default.xml和struts-plugin.xml,这就是其中的原因所在,因为当用户指定后,则系统并不读取默认的配置,则这两文件也无法加载
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(
createXmlConfigurationProvider(file, false));
}
else
{
configurationManager.
addConfigurationProvider(
createStrutsXmlConfigurationProvider(file, false, servletContext));
}
} else {
throw new IllegalArgumentException("Invalid configuration file name");
}
}
}
struts-default.xml,struts-plugin.xml,struts.xml
具体实现代码:
package org.apache.struts2.dispatcher;
/**
* Provide list of default configuration files.
*/
private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";
通过调用下面的方法,加载三个文件字符串
private void init_TraditionalXmlConfigurations(){
//读取web.xml中的init参数config参数,从这儿也可以看出为什么在web.xml中配置时必须写config的原因
String configPaths = initParams.get("config");
//如果configPaths为空,则使用默认,如果用户自己指定,则必须要指定struts-default.xml和struts-plugin.xml,这就是其中的原因所在,因为当用户指定后,则系统并不读取默认的配置,则这两文件也无法加载
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(
createXmlConfigurationProvider(file, false));
}
else
{
configurationManager.
addConfigurationProvider(
createStrutsXmlConfigurationProvider(file, false, servletContext));
}
} else {
throw new IllegalArgumentException("Invalid configuration file name");
}
}
}