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.
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.
- struts-development.xml – Put all development module settings here.
- struts-marketing.xml – Put all marketing module settings here.
- struts-sales.xml – Put all sales module settings here.
- struts.xml -Put default settings and include the struts-development.xml.xml, struts-marketing.xml and 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="development" namespace="/development" extends="struts-default">
<action name="DevelopmentConfig" class="org.websparrow.MultipleConfigAction">
<result name="SUCCESS">/development.jsp</result>
</action>
</package>
</struts>
<?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>
<?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>
<?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
.
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.
<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>
<html>
<head>
<title>Development Configuration</title>
</head>
<body>
<h2>Hi, You are seeing result from Development Configuration file.</h2>
</body>
</html>
<html>
<head>
<title>Marketing Configuration</title>
</head>
<body>
<h2>Hi, You are seeing result from Marketing Configuration file.</h2>
</body>
</html>
<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/
Development Page URL: localhost:8082/Struts2MultipleConfig/development/DevelopmentConfig
Marketing Page URL: localhost:8082/Struts2MultipleConfig/marketing/MarketingConfig
Sales Page URL: localhost:8082/Struts2MultipleConfig/sales/SalesConfig
Download Source Code- struts2-multiple-configuration-file-example.zip