Stuts2에서, org.apache.struts2.dispatcher.Dispatcher 클래스의 init_TraditionalXmlConfigurations 메소드는 init xml 구성을 담당합니다. struts-default.xml, struts-plugin.xml, struts.xml의 3 개의 파일을 검색합니다 (이들은 정수형 DEFAULT_CONFIGURATION_PATHS로 정의되고 있습니다).
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.addContainerProvider(createXmlConfigurationProvider(file, false));
} else {
configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, servletContext));
}
} else {
throw new IllegalArgumentException("Invalid configuration file name");
}
}
}
그런 방법 loadConfigurationFiles에서, 모든 구성 파일의 URL을 얻을 것이다 :
try {
urls = getConfigurationUrls(fileName);
} catch (IOException ex) {
ioException = ex;
}
을 다음과 같은 구현은 구성 파일 'URL을 받으시는 방법 :
protected Iterator<URL> getConfigurationUrls(String fileName) throws IOException {
return ClassLoaderUtil.getResources(fileName, XmlConfigurationProvider.class, false);
}
public static Iterator<URL> getResources(String resourceName, Class callingClass, boolean aggregate) throws IOException {
AggregateIterator<URL> iterator = new AggregateIterator<URL>();
iterator.addEnumeration(Thread.currentThread().getContextClassLoader().getResources(resourceName));
if (!iterator.hasNext() || aggregate) {
iterator.addEnumeration(ClassLoaderUtil.class.getClassLoader().getResources(resourceName));
}
if (!iterator.hasNext() || aggregate) {
ClassLoader cl = callingClass.getClassLoader();
if (cl != null) {
iterator.addEnumeration(cl.getResources(resourceName));
}
}
if (!iterator.hasNext() && (resourceName != null) && ((resourceName.length() == 0) || (resourceName.charAt(0) != '/'))) {
return getResources('/' + resourceName, callingClass, aggregate);
}
return iterator;
}
코드 struts가 어떻게 설정을로드하는지가 위에 있습니다. 당신은 간단한 XML 파일과 같은 파일을 읽을 수 있습니다, 그리고
String filePath = "your struts-config.xml file path";
URL resource = Thread.currentThread().getContextClassLoader().getResource(filePath);
: 수동 스트럿 - config.xml 파일을로드 할 경우 당신을 위해
는 다음과 같은 코드를 사용할 수 있습니다.