How to configure multiple struts configuration file in struts

In this tutorial you will learn how you can configure multiple struts configuration files, there can be need in large web application where it is difficult to maintain the various modules in single struts configuration file, in this case you can split the configuration file into module wise and configure these configuration file in web.xml.

As you know the struts configuration files are specified inside in the web.xml deployment descriptor file when defining the use of the struts ActionServlet, as shown in below code:

<servlet>
   <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

The struts configuration file is specified by declaring a servlet initialization parameter named config. The config parameter must be set to the Web application-relative path of your configuration file, which will be underneath the protected /WEB-INF/ directory so that it can be accessed only from server-side applications.

The struts framework supports the use of multiple configuration files. In this way, configuration settings can be broken down into separate files, which allow teams of developers to work on an application in parallel without having the configuration file be a point of contention. To specify multiple configuration files, simply list each configuration file delimited by commas, as shown in below code:

<init-param>
  <param-name>config</param-name>
  <param-value>
    /WEB-INF/struts-config-module1.xml,
    /WEB-INF/struts-config-module2.xml,
    /WEB-INF/struts-config-module3.xml,
  </param-value>
</init-param>

When Struts loads the configuration files, if there is any overlap among the files’ settings, Struts will use the last settings specified. For example, if configuration file module1 specifies a setting and then configuration file module2 specifies the same setting with a different value, the setting in configuration file module2 will override the one in configuration file module1, because file module2 is loaded after file module1.

In addition to support for multiple configuration files, the Struts framework has support for application modules. The module feature allows applications to be broken down into discrete chunks and can be thought of as being almost a mini-application inside of a large application. Using modules requires you to create a separate configuration file for each module, as shown in below code:

<servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
    <param-name>config</param-name>
    <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <init-param>
    <param-name>config/module1</param-name>
    <param-value>/WEB-INF/struts-config-module1.xml</param-value>
  </init-param>
  <init-param>
    <param-name>config/module2</param-name>
    <param-value>/WEB-INF/struts-config-module2.xml</param-value>
  </init-param>
  <init-param>
    <param-name>config/module3</param-name>
    <param-value>/WEB-INF/struts-config-module3.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

You can notice from above code that the second, third and fourth init-param definitions specify parameters named config/module1, config/module2 and config/module3 respectively. The struts framework uses the part of the name following the slash (/) as the logical name for the module and loads and associates the specified configuration file with that module. So for a parameter named config/module1, the module’s name is module1. For module configuration files, parameter names must begin with config/ in order for Struts to recognize them.

When using modules, you still need to define a default configuration file for your application with the config parameter, as you would with a simple struts application.