public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
String param = filterConfig.getInitParameter("packages");//由此可以看出可以增加参数packages
String packages = "org.apache.struts2.static template org.apache.struts2.interceptor.debugging";
if (param != null) {
packages = param + " " + packages;
}
this.pathPrefixes = parse(packages);
dispatcher = new Dispatcher(filterConfig.getServletContext());
}
private static Settings getDefaultInstance() {
if (defaultImpl == null) {
// Create bootstrap implementation
defaultImpl = new DefaultSettings(); //产生了一个DefaultSettings对象
// Create default implementation
try {
String className = get(StrutsConstants.STRUTS_CONFIGURATION);
if (!className.equals(defaultImpl.getClass().getName())) {
try {
// singleton instances shouldn't be built accessing request or session-specific context data
defaultImpl = (Settings) ObjectFactory.getObjectFactory().buildBean(Thread.currentThread().getContextClassLoader().loadClass(className), null);
} catch (Exception e) {
LOG.error("Could not instantiate settings", e);
}
}
} catch (IllegalArgumentException ex) {
// ignore
}
}
return defaultImpl;
}
public DefaultSettings() {
// Create default implementations
// Use default properties and struts.properties
ArrayList list = new ArrayList();
try {
list.add(new PropertiesSettings("struts"));//从这里加载struts.properties文件
} catch (Exception e) {
log.warn("Could not find or error in struts.properties", e);
}
try {
list.add(new PropertiesSettings("org/apache/struts2/default")); //加载默认的defualt.properties文件
} catch (Exception e) {
log.error("Could not find org/apache/struts2/default.properties", e);
}
Settings[] configList = new Settings[list.size()];//定义了Settings对象数组
config = new DelegatingSettings((Settings[]) list.toArray(configList));
// Add list of additional properties settingss
try {
StringTokenizer configFiles = new StringTokenizer((String) config.getImpl(StrutsConstants.STRUTS_CUSTOM_PROPERTIES), ",");
加载了用户自己定义的properties文件。
while (configFiles.hasMoreTokens()) {
String name = configFiles.nextToken();
try {
list.add(new PropertiesSettings(name));
} catch (Exception e) {
log.error("Could not find " + name + ".properties. Skipping");
}
}
configList = new Settings[list.size()];
config = new DelegatingSettings((Settings[]) list.toArray(configList));
} catch (IllegalArgumentException e) {
// thrown when Settings is unable to find a certain property
// eg. struts.custom.properties in default.properties which is commented
// out
}
// Add additional list of i18n global resource bundles
try {
LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
StringTokenizer bundleFiles = new StringTokenizer((String) config.getImpl(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES), ", ");
while (bundleFiles.hasMoreTokens()) {
String name = bundleFiles.nextToken();
try {
log.info("Loading global messages from " + name);
LocalizedTextUtil.addDefaultResourceBundle(name);
} catch (Exception e) {
log.error("Could not find " + name + ".properties. Skipping");
}
}
} catch (IllegalArgumentException e) {
// struts.custom.i18n.resources wasn't provided
}
}