How to split (modularize) Struts configuration file
- Details
- Written by Nam Ha Minh
- Last Updated on 01 August 2019   |   Print Email
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:
- How to configure Struts framework in web.xml
- Passing parameters from struts.xml to action class
- Reading parameters from web.xml in Struts action class
- How to access HttpServletRequest and HttpServletResponse within Struts2 action class
Other Struts Tutorials:
- Introduction to Struts 2 framework
- Struts beginner tutorial (Eclipse + Tomcat + XML)
- Struts Beginner Tutorial with Annotations
- Struts beginner tutorial with Convention Plugin (zero-configuration)
- How to handle exceptions in Struts
- Send e-mail with attachments in Struts
- Struts File Upload Tutorial
- Struts - Spring - Hibernate Integration Tutorial
Comments