Struts2 is an excellent MVC Framework
The front-end controller of struts2 is a filter:
In web. XML, configure org. Apache. struts2.dispatcher. Ng. Filter. strutsprepareandexecutefilter.
We already know the Tomcat startup process. When Tomcat is started, this filter will be initialized and the init () method is called:
public void init(FilterConfig filterConfig) throws ServletException { InitOperations init = new InitOperations(); try { FilterHostConfig config = new FilterHostConfig(filterConfig); init.initLogging(config); Dispatcher dispatcher = init.initDispatcher(config); init.initStaticContentLoader(config, dispatcher); prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher); execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);this.excludedPatterns = init.buildExcludedPatternsList(dispatcher); postInit(dispatcher, filterConfig); } finally { init.cleanup(); } }
We noticed this Code:
Dispatcher dispatcher = init.initDispatcher(config);
The initdispatcher (config) method in the initoperations class does the following:
/** * Creates and initializes the dispatcher */ public Dispatcher initDispatcher( HostConfig filterConfig ) { Dispatcher dispatcher = createDispatcher(filterConfig); dispatcher.init(); return dispatcher; }
Create a dispatcher object and initialize it. The focus is on the dispatcher. INIT () method. The following is done in this method:
public void init() { if (configurationManager == null) { configurationManager = new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME); } try { init_DefaultProperties(); // [1] init_TraditionalXmlConfigurations(); // [2] init_LegacyStrutsProperties(); // [3] init_CustomConfigurationProviders(); // [5] init_FilterInitParameters() ; // [6] init_AliasStandardObjects() ; // [7] Container container = init_PreloadConfiguration(); container.inject(this); init_CheckConfigurationReloading(container); init_CheckWebLogicWorkaround(container); if (!dispatcherListeners.isEmpty()) { for (DispatcherListener l : dispatcherListeners) { l.dispatcherInitialized(this); } } } catch (Exception ex) { if (LOG.isErrorEnabled()) LOG.error("Dispatcher initialization failed", ex); throw new StrutsException(ex); } }
Init_defaultproperties () This method loads the org/Apache/struts2/Default. properties file;
Init_traditionalxmlconfigurations (); In this method, we can see:
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"); } } }
What is default_configuration_paths ??
private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";
In fact, the above three default configuration files are loaded here. If the same elements exist in these configuration files, the latter overwrites the former in the loading order.
So we can integrate other plug-ins in the form of a jar package through the struts-plugin.xml file, and the jar package contains the struts-plugin.xml file.
We can configure struts2 in the sturts. xml file and include other struts2 configuration files through include.
The init_preloadconfiguration () method parses the loaded configuration file:
private Container init_PreloadConfiguration() { Configuration config = configurationManager.getConfiguration(); Container container = config.getContainer(); boolean reloadi18n = Boolean.valueOf(container.getInstance(String.class, StrutsConstants.STRUTS_I18N_RELOAD)); LocalizedTextUtil.setReloadBundles(reloadi18n); return container; }
Configurationmanager. getconfiguration ()
public synchronized Configuration getConfiguration() { if (configuration == null) { setConfiguration(new DefaultConfiguration(defaultFrameworkBeanName)); try { configuration.reloadContainer(getContainerProviders()); } catch (ConfigurationException e) { setConfiguration(null); throw new ConfigurationException("Unable to load configuration.", e); } } else { conditionalReload(); } return configuration; }
Configuration. reloadcontainer (getcontainerproviders (): parses the configuration file
/** * Calls the ConfigurationProviderFactory.getConfig() to tell it to reload the configuration and then calls * buildRuntimeConfiguration(). * * @throws ConfigurationException */ public synchronized List<PackageProvider> reloadContainer(List<ContainerProvider> providers) throws ConfigurationException
This method is too long to be put on.
As a result, all the configurations in struts2 are loaded and parsed.