Typically we use only struts.xml file to configure a small Struts2 application. For larger and more complex application with many modules, the struts.xml file becomes bulky, thus hard to maintain.

So Struts provides an option to break the struts.xml file into multiple configuration files, using the <include> element in the struts.xml file. Here’s a quick example:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<include file="Common.xml" />
	<include file="Admin.xml" />
	<include file="User.xml" />
	<include file="Product.xml" />
</struts>
As we can see, this struts.xml file includes 4 other configuration file which each corresponds to a separate module of the application: common, admin, user and product. This would help to modularize the application in which each team/developer can work on a particular configuration file independently.

According to Struts2 documentation, the included files must have same format as the struts.xml file (including the DOCTYPE declaration). Here are two examples for the Admin.xml and User.xml files above:

Admin.xml:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="Admin" extends="struts-default">
		<action name="adminHome" class="net.codejava.struts.AdminAction">
			<result name="success">/WEB-INF/views/admin.jsp</result>
		</action>	
	</package>
</struts> 
 

User.xml:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="User" extends="struts-default">
		<action name="userHome" class="net.codejava.struts.UserAction">
			<result name="success">/WEB-INF/views/user.jsp</result>
		</action>
	</package>
</struts>
 

And the included files can be place anywhere in the classpath. Here’s an example of including a configuration file which is inside a package:

<include file="net/codejava/struts/Common.xml" />


And the included file can include another files as well, for example the Product.xml includes Computer.xml as shown below:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="Product" extends="struts-default">
		<action name="productHome" class="net.codejava.struts.ProductAction">
			<result name="success">/WEB-INF/views/product.jsp</result>
		</action>	
	</package>
	
	<include file="Computer.xml" />
	
</struts> 
You can download a demo application in Eclipse project under the attachments section.

 

Related Struts Tutorials:

 

Other Struts Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (Struts2MultipleConfigs.zip)Struts2MultipleConfigs.zip[Eclipse project]3443 kB

Add comment