Struts configuration file implements custom loading in Spring's way

When using struts, we need to configure the filter in web.xml, and we need to configure the struts configuration file path to load the related configuration information of struts in the project. If we do not configure the path, Struts will have some default loading paths, such as a configuration file named struts.xml under the project root path.

However, in actual projects, there are many struts configuration files, and they cannot all be placed in the root path of the project. Generally, they are managed in a unified directory, such as (StrutsCfg directory). When there are many configuration files, there is a The method is to configure a configuration item in the interceptor of sturts

  <param-name>config</param-name>

  <param-value>Configuration file name, multiple separated by commas</param-value>

It should be noted here that when manually configured, struts will not load the default configuration of struts by default. Such as struts-default.xml, struts-plugin.xml. The specific code is as follows

So it is obviously not good to customize the configuration file that loads struts in this way. So we use the second way to load the configuration file in the Spring way.

First we need to provide a custom configuration add-in class for struts

<filter> 
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>configProviders </param-name>
<param-value>Defining a class here needs to inherit XmlConfigurationProvider</param-value>
</init-param>
</filter>
After we define a class by ourselves, we need to inherit XmlConfigurationProvider, and then rewrite this method

You can see that the parameter of the method is a string, here we use a unified resource access interface ResourcePatternResolver provided by Spring, which supports the wildcard path format of classpath*. So if the project has multiple templates, you can load the configuration files under all modules, the specific usage is as follows

The path of the configFilePattern here is

classpath*:strutsCfg/struts-*.xmlSummary 

: This way of loading configuration files can not affect the loading of struts default configuration files, but also realize the loading of Spring wildcard paths, which is simple and convenient, and no need for struts filter configuration. A lot of configuration!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325705513&siteId=291194637