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.

6 years ago, I had joined a large Struts development project which involve 20+ modules. Unfortunately, the prior developers put all the Struts related stuff (action, form and etc) into a single Struts configuration file (struts-config.xml). The struts-config.xml just keep growing extremely fast and finally hit 20++mb, every update to this configuration file will take few minutes, and even wait half an hour for a single debugging deployment in Eclipse IDE. This is a serious performance issue and causing the project keep delay the production date. What a good Struts development experience.

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.

Struts-mutiple-configuration-file

1. Single module

A single module support multiple Struts configuration files.

page1.jsp



This is Page 1

page2.jsp



This is Page 2

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
  1. http://localhost:8080/StrutsExample/Page1.do
    It will display the page1.jsp
  2. 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



Welcome to admin page

common/welcome.jsp



Welcome to common page

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.

In Struts 2, the “Namespace” is a more efficient way to replace this “config parameter” setting.

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/

  1. http://localhost:8080/StrutsExample/admin/Welcome.do
    It will display the admin/welcome.jsp
  2. http://localhost:8080/StrutsExample/common/Welcome.do
    It will display the common/welcome.jsp

Each modules has own Struts configuration file.

Download Source Code

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
22 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
20 Comment authors
Aung Myo ShaneRobertSINGHDenischaitanya Recent comment authors
newest oldest most voted
Mj
Guest
Mj

You seem to have made a mistake in the results, you mentioned
as such

“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 ”

But the the “http://localhost:8080/StrutsExample/common/Welcome.do” will not yield the page2.jsp, rather it would show the “Welcome to common page” message.

Could you please correct this.

Good job on the neat and clean tutorial style.

P.S – Please checkout for the mistake pointed out by “GeekDude”, I was a bit confused when I was doing this.

candreshwar
Guest
candreshwar

How multiple configuration files will solve the performance issue..?

trackback
Struts Tutorials | J2EE Web Development Tutorials

[…] 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. […]

trackback
Struts 2 Namespace configuration example and explanation | J2EE Web Development Tutorials

[…] 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 […]

trackback
Struts 2 Hello World Example

[…] 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 […]

trackback
Struts 2 – Include multiple Struts configuration files

[…] 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. […]

trackback
How to split faces-config.xml into multiple files?

[…] … </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 […]

Tapan
Guest
Tapan

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… Read more »

Amir
Guest
Amir

A big thank. You solved a big problem of mine.

Githin
Guest
Githin

It was a new knowledge. Thanks.

pakar smo
Guest
pakar smo

It’s really a nice and useful piece of info. I’m happy that you shared this helpful info with us. Please keep us up to date like this. Thank you for sharing.

GeekDude
Guest
GeekDude

i found small text mistache here: 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

here “struts-config-admin.xml” given twice instead of “struts-config-common.xml”.

Could you please correct this.

brahma
Guest
brahma

HTTP Status 500 – No action instance for path /Activation could be created

——————————————————————————–

type Status report

message No action instance for path /Activation could be created

description The server encountered an internal error (No action instance for path /Activation could be created) that prevented it from fulfilling this request.

——————————————————————————–

Apache Tomcat/6.0.35

——————————————–
when i run the code above proble is occured..
i had provided the action path in struts-config.xml
————————————————–
when the same code is used to the others which had given the path older it is working..please provide me help of this problem..
thanks

mallesham
Guest
mallesham

Hi MK,

It is very good for learning how to configure multiple struts config files.
I have one question regarding the 2nd method.
It looks struts access the jsp files configured in the config files by prefixing module name (admin and common)
By this way how to configure the jsp files under the /WEB-INF directory

Thanks,
Mallesham

Naga Srinivas
Guest
Naga Srinivas

Hi,
Very good tutorial to learn how to configure multiple Configuration files.
your presentation is very good.

keep it up.

Thank You..

Ali
Guest
Ali

Hi,

How do I compile this project and run it?
Thanks.

Shubhrajyoti satpathy
Guest
Shubhrajyoti satpathy

how i want to run struts in web logic ?

Pierre-Yves
Guest
Pierre-Yves

Hi,

Thanks for this. I have one question though I can’t find the answer anywhere:
what happens if you have the same action path in 2 config file that you set for the same module ?
Is there a king of extend ? That is actually what I want to do: I want a default behavior for most actions in my diferents modules, but a specific one for a few.
This would look like this:
config/module1
struts-default, struts_module1
config/module2
struts-default, struts_module2

Is that possible ? What about the same thing with tiles ?

raghu
Guest
raghu

It?s really a nice and useful piece of information. I?m happy that you shared this helpful information with us. Please keep us up to date like this.
Thank you for sharing.

chaitanya
Guest
chaitanya

sir, i learnt so many things from your tutorials, but have one doubt,

what will be the advantage of making different config files??? because all the different files would be contain the same code as the one single file.. and also the size of all files would same as the one file…

thanks in advance for your answer..

Denis
Guest
Denis

Hola) Why i can’t use only specific config files for action servlet i.e. with “param-name” like “config/user” and “config/admin”. In my case for runing application i necessarily need a default param-name “config”? Can i use only specific config files? Thank you.

SINGH
Guest
SINGH

I think there is no ForwardAction class under struts1.3.10 . it is avaiable in struts 1.2.9 jar That is (org.apache.struts.actions.ForwadAction)

Robert
Guest
Robert

Hi. way “struts-config-admin.xml, struts-config-admin.xml” twice !

download have not source code !!!