Struts 2 Multiple Configuration File Example


If you are planning to develop a large-scale Struts based application, you must aware with the include file functionality of Struts configuration file (struts.xml).

<include file="development/struts-development.xml"></include>
<include file="marketing/struts-marketing.xml"></include>
<include file="sales/struts-sales.xml"></include>

Now you can create multiple configuration files in a Struts2 application using include file functionality.

Note: If you are thinking about struts.xml file so you can not create such multiple file.

Create the separate configuration file for each module and include all in core configuration file struts.xml.

Have a look at project structure.

Struts 2 Multiple Configuration File Example

Multiple Struts configuration files

In my demo, I have created three modules development, marketing, and sales. So we create the separate configuration file for each module.

  1. struts-development.xml – Put all development module settings here.
  2. struts-marketing.xml – Put all marketing module settings here.
  3. struts-sales.xml – Put all sales module settings here.
  4. struts.xml -Put default settings and include the struts-development.xml.xml, struts-marketing.xml and struts-sales.xml.
struts-development.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.devMode" value="true" />
	<package name="development" namespace="/development" extends="struts-default">
		<action name="DevelopmentConfig" class="org.websparrow.MultipleConfigAction">
			<result name="SUCCESS">/development.jsp</result>
		</action>
	</package>
</struts>
struts-marketing.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.devMode" value="true" />
	<package name="marketing" namespace="/marketing" extends="struts-default">
		<action name="MarketingConfig" class="org.websparrow.MultipleConfigAction">
			<result name="SUCCESS">/marketing.jsp</result>
		</action>
	</package>
</struts>
struts-sales.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.devMode" value="true" />
	<package name="sales" namespace="/sales" extends="struts-default">
		<action name="SalesConfig" class="org.websparrow.MultipleConfigAction">
			<result name="SUCCESS">/sales.jsp</result>
		</action>
	</package>
</struts>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

	<constant name="struts.devMode" value="true" />

	<package name="default" namespace="/" extends="struts-default">
	</package>

	<include file="development/struts-development.xml"></include>
	<include file="marketing/struts-marketing.xml"></include>
	<include file="sales/struts-sales.xml"></include>

</struts>

Action Class

Create an action class and override execute() method and return the SUCCESS.

MultipleConfigAction.java
package org.websparrow;

import com.opensymphony.xwork2.ActionSupport;

public class MultipleConfigAction extends ActionSupport {

	private static final long serialVersionUID = -3695898710628233998L;

	@Override
	public String execute() throws Exception {

		return "SUCCESS";
	}
}

View Components

Create the JSP pages for each module.

index.jsp
<html>
	<head>
		<title>Welcome</title>
	</head>
	<body>
		<h1>Struts 2 Multiple Configuration File Example</h1>
		<p>Click Me</p>
		<ul>
			<li><a href="development/DevelopmentConfig">Development</a></li>
			<li><a href="marketing/MarketingConfig">Marketing</a></li>
			<li><a href="sales/SalesConfig">Sales</a></li>
		</ul>
	</body>
</html>
developmet.jsp
<html>
<head>
<title>Development Configuration</title>
</head>
<body>
	<h2>Hi, You are seeing result from Development Configuration file.</h2>
</body>
</html>
marketing.jsp
<html>
<head>
<title>Marketing Configuration</title>
</head>
<body>
	<h2>Hi, You are seeing result from Marketing Configuration file.</h2>
</body>
</html>
sales.jsp
<html>
<head>
<title>Sales Configuration</title>
</head>
<body>
	<h2>Hi, You are seeing result from Sales Configuration file.</h2>
</body>
</html>

Output:

Deploy the project inside Tomcat » webapps directory and hit the URL one by one, you will get the response from different configuration file each time.

Home Page URL: localhost:8082/Struts2MultipleConfig/

Struts 2 Multiple Configuration File Example

Development Page URL: localhost:8082/Struts2MultipleConfig/development/DevelopmentConfig

Struts 2 Multiple Configuration File Example

Marketing Page URL: localhost:8082/Struts2MultipleConfig/marketing/MarketingConfig

Struts 2 Multiple Configuration File Example

Sales Page URL: localhost:8082/Struts2MultipleConfig/sales/SalesConfig

Struts 2 Multiple Configuration File Example

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.