Struts – Multiple configuration files example
Many developers like to put all Struts related stuff (action, form) into a single Struts configuration file. It’s fast for the initial development but bad for the future maintenance, and may be those developers are not aware of the Struts is allow multiple configuration files feature.
Please split the Struts configuration details into different modules, Struts can do it easily.
Struts multiple configuration files example
This is the sample project structure for the demonstration.

1. Single module
A single module support multiple Struts configuration files.
page1.jsp
<html> <body> <h1>This is Page 1</h1> </body> </html>
page2.jsp
<html> <body> <h1>This is Page 2</h1> </body> </html>
struts-config-1.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <action-mappings> <action path="/Page1" type="org.apache.struts.actions.ForwardAction" parameter="/pages/page1.jsp"/> </action-mappings> </struts-config>
struts-config-2.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <action-mappings> <action path="/Page2" type="org.apache.struts.actions.ForwardAction" parameter="/pages/page2.jsp"/> </action-mappings> </struts-config>
In the web.xml, you can separate multiple Struts configure file by a comma “,“.
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Maven Struts Examples</display-name> <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-1.xml, /WEB-INF/struts-config-2.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
Test it
- http://localhost:8080/StrutsExample/Page1.do
It will display the page1.jsp - http://localhost:8080/StrutsExample/common/Welcome.do
It will display the page2.jsp
Both Struts configuration are loaded property.
2. Multiple modules
Multiple modules, each has own Struts configuration files.
admin/welcome.jsp
<html> <body> <h1>Welcome to admin page</h1> </body> </html>
common/welcome.jsp
<html> <body> <h1>Welcome to common page</h1> </body> </html>
Both “struts-config-admin.xml” and “struts-config-admin.xml” files contains the same settings, Struts is able to differential it via the “config” parameter value in web.xml.
struts-config-admin.xml, struts-config-admin.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <action-mappings> <action path="/Welcome" type="org.apache.struts.actions.ForwardAction" parameter="/welcome.jsp"/> </action-mappings> </struts-config>
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Maven Struts Examples</display-name> <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-1.xml, /WEB-INF/struts-config-2.xml </param-value> </init-param> <init-param> <param-name>config/admin</param-name> <param-value> /WEB-INF/struts-config-admin.xml </param-value> </init-param> <init-param> <param-name>config/common</param-name> <param-value> /WEB-INF/struts-config-common.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
Test it
The “config/admin” will match to this URL pattern – http://localhost:8080/StrutsExample/admin/
The “config/common” will match to this URL pattern – http://localhost:8080/StrutsExample/common/
- http://localhost:8080/StrutsExample/admin/Welcome.do
It will display the admin/welcome.jsp - http://localhost:8080/StrutsExample/common/Welcome.do
It will display the common/welcome.jsp
Each modules has own Struts configuration file.
Hi MK,
Nice article…I have one query regarding method invocation.
Suppose i want to call method dynamically and that method name will come from DB. so the scenario is, there is one Master module and 2 sub modules lets say M1 and M2, Now every request from any module will go through Master Module, so if module M1 ask for method name suppose getName() which exist in M2 action class only (but M1 is not aware about it) then first he will send a request to Master Module (with method name as a parameter) then master module will find out for which method this request is for and then he will call M2 getName() method. To do this we dont need to Map method getName() into Struts.xml, We have to set this in Action class of Master Module. I am not able to understand how to do this Cause every time Master Module will get some method name (as Parameter) and will call respective action which include that method name.
Please Help
Thanks
Tapan
[...] … </web-app> Note For Struts developer, this is the exact classic problem happened in the Struts configuration file as well. This article was posted in JSF2 [...]
[...] which is not recommend and MUST BE AVOID. Do not think this is a case study, it did happened in real life. I seen many Struts 1 or 2 developers just group everything in a single Struts configuration file. [...]
[...] In Struts 2, you can access the action class directly with a suffix of .action. Struts 2 Namespaces are the equivalent of Struts 1 multiple modules [...]
[...] Struts 2 Namespace configuration example and explanation Written on June 7, 2010 at 10:05 am by mkyong Struts 2 Namespace is a new concept to handle the multiple modules by given a namespace to each module. In addition, it can used to avoid conflicts between same action names located at different modules. Download It – Struts2-NameSpace-Configuration-Example.zip Struts 2 Namespaces are the equivalent of Struts 1 multiple modules [...]
[...] Multiple Struts configuration files Mutiple Struts configuration files are required in large project environment, here’s an example to show how to configure the multiple Struts configuration file. [...]